<?php
namespace app\index\controller;

use app\index\model\AccdayListModel;
use app\index\model\AccdayModel;
use app\index\model\AccdayPiaoModel;
use app\index\model\AccdayTwoModel;
use app\index\model\AcckuanModel;
use app\index\model\AccountDiaoModel;
use app\index\model\AccountListModel;
use app\index\model\AccountModel;
use app\index\model\BidModel;
use app\index\model\DealcomGengModel;
use app\index\model\DealcomListModel;
use app\index\model\DealcomModel;
use app\index\model\DealcomZhengModel;
use app\index\model\DeptModel;
use app\index\model\EnginBudshouModel;
use app\index\model\EnginBudsubModel;
use app\index\model\EnginChuModel;
use app\index\model\EnginModel;
use app\index\model\FileModel;
use app\index\model\FlowBillModel;
use app\index\model\FlowLogModel;
use app\index\model\HouChujinGuanModel;
use app\index\model\HouChujinListModel;
use app\index\model\HouDayzhiGuanModel;
use app\index\model\HouDayzhiListModel;
use app\index\model\HouGonghuiListModel;
use app\index\model\HouGongzhiGuanModel;
use app\index\model\HouGongzhiListModel;
use app\index\model\IncomeEnginModel;
use app\index\model\IncomeQiModel;
use app\index\model\PaymentBaoModel;
use app\index\model\PaymentDayListModel;
use app\index\model\PaymentEnginModel;
use app\index\model\PaymentGongListModel;
use app\index\model\PiaoChuListModel;
use app\index\model\PiaoKaiListModel;
use app\index\model\PiaokaiModel;
use app\index\model\PiaoShouListModel;
use app\index\model\QianPiaoListModel;
use app\index\model\QianZhiGuanModel;
use app\index\model\QianZhiListModel;
use app\index\model\UserModel;
use app\index\service\BudgetService;
use app\index\service\EnginService;
use app\index\service\FinanceService;
use app\index\service\FlowService;
use app\index\service\KuanService;
use app\index\service\MainService;
use app\index\service\MenuService;
use app\index\service\OptService;
use app\index\service\PaymentService;
use app\index\service\UserService;
use think\Db;
use think\Request;

class Finance extends Common
{
    public function list_dealcom(Request $request){
        $param = $request->post();
        get_page($param);

        $where[] = ['deal.deleted_user','=', 0];
        $where[] = ['deal.comid','=',$this->userinfo['comid']];
        $where[] = ['deal.isturn','=',1];
        $where[] = ['deal.status','in',[0,1]];

        if(isset($param['keyword']) && !empty($param['keyword'])){
            $where[] = ['deal.name|deal.lianer|deal.liantel|deal.lianaddr|deal.legaler','like', '%'.$param['keyword'].'%'];
        }
        if(isset($param['name']) && !empty($param['name'])){
            $where[] = ['deal.name','like', '%'.$param['name'].'%'];
        }
        if(isset($param['lianer']) && !empty($param['lianer'])){
            $where[] = ['deal.lianer','like', '%'.$param['lianer'].'%'];
        }
        if(isset($param['liantel']) && !empty($param['liantel'])){
            $where[] = ['deal.liantel','like', '%'.$param['liantel'].'%'];
        }
        if(isset($param['lianaddr']) && !empty($param['lianaddr'])){
            $where[] = ['deal.lianaddr','like', '%'.$param['lianaddr'].'%'];
        }
        if(isset($param['type']) && !empty($param['type'])){
            $where[] = ['deal.type','=', $param['type']];
        }
        if(isset($param['nature']) && !empty($param['nature'])){
            $where[] = ['deal.nature','=', $param['nature']];
        }
        if(isset($param['legaler']) && !empty($param['legaler'])){
            $where[] = ['deal.legaler','like', '%'.$param['legaler'].'%'];
        }
        if(isset($param['taxtype']) && !empty($param['taxtype'])){
            $where[] = ['deal.taxtype','=', $param['taxtype']];
        }
        if(isset($param['qianpiaotype']) && !empty($param['qianpiaotype'])){
            $where[] = ['deal.qianpiaotype','=', $param['qianpiaotype']];
        }
        if(isset($param['status']) && $param['status'] != ''){
            $where[] = ['deal.status','=', $param['status']];
        }

        $data['type'] = (new OptService())->get_base_info('cai_dealcom_type');
        $data['is_type'] = isset($param['type']) ? $param['type'] : '';

        $data['taxtype'] = (new OptService())->get_base_info('cai_dealcom_taxtype');
        $data['is_taxtype'] = isset($param['taxtype']) ? $param['taxtype'] : '';

        $data['nature'] = (new OptService())->get_base_info('cai_dealcom_nature');
        $data['is_nature'] = isset($param['nature']) ? $param['nature'] : '';

        $data['qianpiaotype'] = (new OptService())->get_base_info('cai_qianpiaotype');
        $data['is_qianpiaotype'] = isset($param['qianpiaotype']) ? $param['qianpiaotype'] : '';


        $data['data'] = DealcomModel::alias('deal')
            ->field('deal.*')
            ->where($where)
            ->where((new MenuService())->get_miss_where('dealcom', 'deal'))
            ->order('deal.id DESC')
            ->paginate($param['pagesize'],false,['page'=>$param['page']])
            ->each(function ($item,$key){
                $item['type'] = (new OptService())->get_base_showname('cai_dealcom_type', $item['type']);
                $item['nature'] = (new OptService())->get_base_showname('cai_dealcom_nature', $item['nature']);
                $item['taxtype'] = (new OptService())->get_base_showname('cai_dealcom_taxtype', $item['taxtype']);
                $item['danlevel'] = (new OptService())->get_base_showname('cai_dealcom_danlevel', $item['danlevel']);
                $item['qianpiaotype'] = (new OptService())->get_base_showname('cai_qianpiaotype', $item['qianpiaotype']);
                return $item;
            })->toArray();

        $data['butauth'] = (new MenuService())->get_butauth('dealcom');
        return outputJson(2000,lang('code_2000'),$data);
    }
    public function add_dealcom(Request $request){
        if($request->isPost()){
            $param = $request->post();

            if(empty($param['name'])){
                return outputJson(4000, '请输入单位名称');
            }
            if((new FinanceService())->check_dealcom_name($param['name'])){
                return outputJson(4000, '单位名称已存在');
            }
            if(empty($param['type'])){
                return outputJson(4000, '请选择所属分类');
            }
            if(empty($param['nature'])){
                return outputJson(4000, '请选择单位性质');
            }



            $param['comid'] = $this->userinfo['comid'];
            $param['status'] = 1;
            $param['isturn'] = 1;
            $param['created_user'] = $param['updated_user'] = $this->userinfo['id'];
            $param['created_at'] = $param['updated_at']= date('Y-m-d H:i:s');
            $param['deptid'] = $this->userinfo['deptid'];
            $param['chanid'] = (new UserService())->get_user_chanid();

            $files = array_filter($param['files']);
            unset($param['files']);
            unset($param['token']);

            Db::startTrans();
            try {
                $id = DealcomModel::insertGetId($param);
                (new OptService())->relation_file($files,'dealcom',$id);
                (new OptService())->add_flow_log(FlowLogModel::SET_ADD, 'dealcom', $id);
                (new OptService())->add_log_list(FlowLogModel::SET_ADD, 'dealcom', $id, '往来单位');
                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,lang('code_4000'));
            }
            return outputJson(2000,lang('code_2000'));
        }else{
            $data['type'] = (new OptService())->get_base_info('cai_dealcom_type');
            $data['taxtype'] = (new OptService())->get_base_info('cai_dealcom_taxtype');
            $data['nature'] = (new OptService())->get_base_info('cai_dealcom_nature');
            $data['danlevel'] = (new OptService())->get_base_info('cai_dealcom_danlevel');
            $data['files'] = (new OptService())->get_file('dealcom');
            return outputJson(2000,lang('code_2000'), $data);
        }
    }
    public function add_dealcom_list(Request $request){
        if($request->isPost()){
            $param = $request->post();

            if(empty($param['zhangname'])){
                return outputJson(4000, '请输入账户名');
            }
            if(empty($param['bankname'])){
                return outputJson(4000, '请输入开户行');
            }
            if(empty($param['banknum'])){
                return outputJson(4000, '请输入银行账号');
            }

            unset($param['token']);

            Db::startTrans();
            try {

//                if($param['isdea'] > 0){
//
//                }


                $id = DealcomListModel::insertGetId($param);
                (new OptService())->add_flow_log('新增银行账户信息', 'dealcom', $param['dealcomid']);
                (new OptService())->add_log_list('新增银行账户信息', 'dealcom', $param['dealcomid'], '往来单位');
                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,$e->getMessage());
                return outputJson(4000,lang('code_4000'));
            }
            return outputJson(2000,lang('code_2000'));
        }else{
            $id = $request->get('dealcomid',0,'int');
            $data['data'] = DealcomModel::where(['id'=>$id])->find();
            if(!$data['data']){
                return outputJson(4000, lang('code_4000'));
            }
            return outputJson(2000,lang('code_2000'), $data);
        }
    }
    public function add_dealcom_zheng(Request $request){
        if($request->isPost()){
            $param = $request->post();

            if(empty($param['name'])){
                return outputJson(4000, '请输入证件名称');
            }
            if(empty($param['number'])){
                return outputJson(4000, '请输入证件编号');
            }


            unset($param['token']);

            Db::startTrans();
            try {
                $id = DealcomZhengModel::insertGetId($param);
                (new OptService())->add_flow_log('新增证件信息', 'dealcom', $param['dealcomid']);
                (new OptService())->add_log_list('新增证件信息', 'dealcom', $param['dealcomid'], '往来单位');
                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,$e->getMessage());
                return outputJson(4000,lang('code_4000'));
            }
            return outputJson(2000,lang('code_2000'));
        }else{
            $id = $request->get('dealcomid',0,'int');
            $data['data'] = DealcomModel::where(['id'=>$id])->find();
            if(!$data['data']){
                return outputJson(4000, lang('code_4000'));
            }
            return outputJson(2000,lang('code_2000'), $data);
        }
    }
    public function edit_dealcom(Request $request){
        if($request->isPost()){
            $param = $request->post();

            if(empty($param['name'])){
                return outputJson(4000, '请输入单位名称');
            }
            if((new FinanceService())->check_dealcom_name($param['name'], $param['id'])){
                return outputJson(4000, '单位名称已存在');
            }
            if(empty($param['type'])){
                return outputJson(4000, '请选择所属分类');
            }

            $param['updated_user'] = $this->userinfo['id'];
            $param['updated_at'] = date('Y-m-d H:i:s');
            $files = array_filter($param['files']);
            unset($param['files']);
            unset($param['token']);


            $list = array_filter($param['list']);
            unset($param['list']);

            $zheng = array_filter($param['zheng']);
            unset($param['zheng']);


            if(count($list) > 0){
                $isdea = 0;
                foreach ($list as $vvv){
                    if($vvv['isdea'] > 0){
                        $isdea = 1;
                    }
                }
                if($isdea < 1){
                    return outputJson(4000, '账户信息,请选择默认账户');
                }
            }

            if(count($zheng) > 0){
                $isdea = 0;
                foreach ($zheng as $vvv){
                    if($vvv['isdea'] > 0){
                        $isdea = 1;
                    }
                }
                if($isdea < 1){
                    return outputJson(4000, '证件信息,请选择默认证件');
                }
            }


            Db::startTrans();
            try {
                DealcomModel::update($param);

                DealcomListModel::where(['dealcomid'=>$param['id']])->delete();
                foreach($list as $vv){
                    $vv['dealcomid'] = $param['id'];
                    DealcomListModel::insert($vv);
                }

                DealcomZhengModel::where(['dealcomid'=>$param['id']])->delete();
                foreach($zheng as $vvv){
                    $vvv['dealcomid'] = $param['id'];
                    DealcomZhengModel::insert($vvv);
                }


                (new OptService())->relation_file($files,'dealcom',$param['id']);
                (new OptService())->add_flow_log(FlowLogModel::SET_EDIT, 'dealcom', $param['id']);
                (new OptService())->add_log_list(FlowLogModel::SET_EDIT, 'dealcom', $param['id'], '往来单位');
                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,lang('code_4000'));
            }
            return outputJson(2000,lang('code_2000'));
        }else{
            $id = $request->get('id',0,'int');
            $data['data'] = DealcomModel::where(['id'=>$id])->find();
            if(!$data['data']){
                return outputJson(4000, lang('code_4000'));
            }

            $list = DealcomListModel::where(['dealcomid'=>$data['data']['id']])->select()->toArray();
            $data['list'] = $list;


            $zheng = DealcomZhengModel::where(['dealcomid'=>$data['data']['id']])->select()->toArray();
            $data['zheng'] = $zheng;

            $data['type'] = (new OptService())->get_base_info('cai_dealcom_type');
            $data['taxtype'] = (new OptService())->get_base_info('cai_dealcom_taxtype');
            $data['nature'] = (new OptService())->get_base_info('cai_dealcom_nature');
            $data['danlevel'] = (new OptService())->get_base_info('cai_dealcom_danlevel');
            $data['qianpiaotype'] = (new OptService())->get_base_info('cai_qianpiaotype');
            $data['files'] = (new OptService())->get_file('dealcom', $data['data']['id']);
            return outputJson(2000,lang('code_2000'),$data);
        }
    }
    public function del_dealcom(Request $request){
        $param = $request->post();
        $data = DealcomModel::get_info_by_pk($param['id']);
        if(!$data){
            return outputJson(4000,lang('code_4000'));
        }

        Db::startTrans();
        try {
            FlowBillModel::where(['id'=>$data['billid']])->update([
                'deleted_user' => $this->userinfo['id'],
                'deleted_at' => date('Y-m-d H:i:s')
            ]);
            DealcomModel::where(['id'=>$data['id']])->update([
                'deleted_user' => $this->userinfo['id'],
                'deleted_at' => date('Y-m-d H:i:s')
            ]);

            (new OptService())->add_log_list(FlowLogModel::SET_DELETE, 'dealcom', $data['id'], '往来单位');
            Db::commit();
        } catch (\Exception $e) {
            Db::rollback();
            return outputJson(4000,lang('code_4000'));
        }
        return outputJson(2000,lang('code_2000'));
    }
    public function show_dealcom(Request $request){
        $id = $request->post('id', 0, 'int');
        $data['data'] = DealcomModel::get_info_by_pk($id);
        if(!$data['data']){
            return outputJson(4000, lang('code_4000'));
        }

        $data['data']['qianpiaotype_name'] = (new OptService())->get_base_showname('cai_qianpiaotype', $data['data']['qianpiaotype']);
        $data['data']['type'] = (new OptService())->get_base_showname('cai_dealcom_type', $data['data']['type']);
        $data['data']['nature'] = (new OptService())->get_base_showname('cai_dealcom_nature', $data['data']['nature']);
        $data['data']['taxtype'] = (new OptService())->get_base_showname('cai_dealcom_taxtype', $data['data']['taxtype']);
        $data['data']['danlevel'] = (new OptService())->get_base_showname('cai_dealcom_danlevel', $data['data']['danlevel']);

        $list = DealcomListModel::where(['dealcomid'=>$data['data']['id']])->select()->toArray();
        foreach ($list as &$item){
            $item['isdea_name'] = $item['isdea'] > 0 ? '是' : '否';
        }
        $data['list'] = $list;



        $zheng = DealcomZhengModel::where(['dealcomid'=>$data['data']['id']])->select()->toArray();
        foreach ($zheng as &$itemvv){
            $itemvv['isdea_name'] = $itemvv['isdea'] > 0 ? '是' : '否';
        }
        $data['zheng'] = $zheng;

        $data['files'] = (new OptService())->get_file('dealcom', $data['data']['id']);
        $data['billinfo'] = (new FlowService())->get_flow_show_info($data['data']['billid']);
        return outputJson(2000,lang('code_2000'), $data);
    }
    public function show_dealcom_geng(Request $request){
        $id = $request->post('id', 0, 'int');
        $data['data'] = DealcomGengModel::get_info_by_pk($id);
        if(!$data['data']){
            return outputJson(4000, lang('code_4000'));
        }

        $dealid_info = DealcomModel::where(['id'=>$data['data']['dealid']])->find();
        $data['dealid_info'] = $dealid_info;

        $data['data']['type'] = (new OptService())->get_base_showname('cai_dealcom_type', $data['data']['type']);
        $data['data']['piaotype'] = (new OptService())->get_base_showname('cai_dealcom_piaotype', $data['data']['piaotype']);
        $data['data']['nature'] = (new OptService())->get_base_showname('cai_dealcom_nature', $data['data']['nature']);
        $data['data']['taxtype'] = (new OptService())->get_base_showname('cai_dealcom_taxtype', $data['data']['taxtype']);
        $data['data']['danlevel'] = (new OptService())->get_base_showname('cai_dealcom_danlevel', $data['data']['danlevel']);
        $data['files'] = (new OptService())->get_file('dealcom_geng', $data['data']['id']);
        $data['billinfo'] = (new FlowService())->get_flow_show_info($data['data']['billid']);
        return outputJson(2000,lang('code_2000'), $data);
    }

    public function list_account(Request $request){
        $param = $request->post();
        get_page($param);

        $where[] = ['acc.deleted_user','=', 0];
        $where[] = ['acc.comid','=',$this->userinfo['comid']];

        if(isset($param['keyword']) && !empty($param['keyword'])){
            $where[] = ['acc.name|acc.accname|acc.accnum','like', '%'.$param['keyword'].'%'];
        }
        if(isset($param['name']) && !empty($param['name'])){
            $where[] = ['acc.name','like', '%'.$param['name'].'%'];
        }
        if(isset($param['accname']) && !empty($param['accname'])){
            $where[] = ['acc.accname','like', '%'.$param['accname'].'%'];
        }
        if(isset($param['accnum']) && !empty($param['accnum'])){
            $where[] = ['acc.accnum','like', '%'.$param['accnum'].'%'];
        }
        if(isset($param['acctype']) && !empty($param['acctype'])){
            $where[] = ['acc.acctype','=', $param['acctype']];
        }
        if(isset($param['accmoney']) && !empty($param['accmoney'])){
            $where[] = ['acc.accmoney','like', '%'.$param['accmoney'].'%'];
        }
        if(isset($param['accdt']) && !empty($param['accdt'])){
            $where[] = ['acc.accdt','between', explode(' - ', $param['accdt'])];
        }
        if(isset($param['chanid']) && !empty($param['chanid'])){
            $where[] = ['acc.chanid','=', $param['chanid']];
        }
        $data['chanid'] = (new UserService())->get_chaninfo_all();;
        $data['is_chanid'] = isset($param['chanid']) ? $param['chanid'] : '';

        $data['acctype'] = (new OptService())->get_base_info('cai_account_acctype');
        $data['is_acctype'] = isset($param['acctype']) ? $param['acctype'] : '';



        $whereJiao = [];
        if(isset($param['dt']) && !empty($param['dt'])){
            $whereJiao[] = ['list.dt','between', explode(' - ', $param['dt'])];
        }

        $data['data'] = AccountModel::alias('acc')
            ->field('acc.*,e.name as enginname')
            ->leftJoin('engin e','e.id=acc.enginid')
            ->where($where)
            ->where((new MenuService())->get_miss_where('account', 'acc'))
            ->order('acc.id DESC')
            ->paginate($param['pagesize'],false,['page'=>$param['page']])
            ->each(function ($item,$key)use($whereJiao){
                $item['acctype'] = (new OptService())->get_base_showname('cai_account_acctype', $item['acctype']);
                $item['chanid_name'] = DeptModel::where(['id'=>$item['chanid']])->value('name');

                $item['shou'] = AccountListModel::alias('list')
                    ->join('acckuan kuan','kuan.id=list.kuanid')
                    ->where(['list.comid'=>$this->userinfo['comid']])
                    ->where('kuan.modtype','<',10)
                    ->where('list.accid','=',$item['id'])
                    ->where($whereJiao)
                    ->sum('list.money');
                $item['zhi'] = AccountListModel::alias('list')
                    ->join('acckuan kuan','kuan.id=list.kuanid')
                    ->where(['list.comid'=>$this->userinfo['comid']])
                    ->where('kuan.modtype','>',10)
                    ->where('list.accid','=',$item['id'])
                    ->where($whereJiao)
                    ->sum('list.money');


//                $item['shou'] = AccountListModel::where(['accid'=>$item['id']])->where($whereJiao)->where('type','in',[1,2,8,9,11,14])->sum('money');
//                $item['zhi'] = AccountListModel::where(['accid'=>$item['id']])->where($whereJiao)->where('type','not in',[1,2,8,9,11,14])->sum('money');
                $item['cha'] = round($item['shou'] - $item['zhi'], 2);
                $item['balance'] = round($item['accmoney'] + $item['shou'] - $item['zhi'], 2);

                return $item;
            })->toArray();

        $data['butauth'] = (new MenuService())->get_butauth('account');
        return outputJson(2000,lang('code_2000'),$data);
    }
    public function add_account(Request $request){
        if($request->isPost()){
            $param = $request->post();

            if(empty($param['name'])){
                return outputJson(4000, '请输入账户名称');
            }
            if(empty($param['accname'])){
                return outputJson(4000, '请输入开户行');
            }
            if(empty($param['accnum'])){
                return outputJson(4000, '请输入账号');
            }
            if(empty($param['acctype'])){
                return outputJson(4000, '请选择账户性质');
            }
            if($param['accmoney'] == ''){
                return outputJson(4000, '请输入期初金额');
            }
            if(empty($param['accdt'])){
                return outputJson(4000, '请选择期初日期');
            }

            $param['comid'] = $this->userinfo['comid'];
            $param['status'] = 1;
            $param['created_user'] = $param['updated_user'] = $this->userinfo['id'];
            $param['created_at'] = $param['updated_at']= date('Y-m-d H:i:s');
            $param['deptid'] = $this->userinfo['deptid'];

            $files = array_filter($param['files']);
            unset($param['files']);
            unset($param['token']);


            Db::startTrans();
            try {
                $id = AccountModel::insertGetId($param);
                (new OptService())->relation_file($files,'account',$id);
                (new OptService())->add_flow_log(FlowLogModel::SET_ADD, 'account', $id);
                (new OptService())->add_log_list(FlowLogModel::SET_ADD, 'account', $id, '资金账户');
                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,$e->getMessage());
                return outputJson(4000,lang('code_4000'));
            }
            return outputJson(2000,lang('code_2000'));
        }else{
            $data['acctype'] = (new OptService())->get_base_info('cai_account_acctype');
            $data['files'] = (new OptService())->get_file('account');
            return outputJson(2000,lang('code_2000'), $data);
        }
    }
    public function edit_account(Request $request){
        if($request->isPost()){
            $param = $request->post();
            if(empty($param['name'])){
                return outputJson(4000, '请输入账户名称');
            }
            if(empty($param['accname'])){
                return outputJson(4000, '请输入开户行');
            }
            if(empty($param['accnum'])){
                return outputJson(4000, '请输入账号');
            }
            if(empty($param['acctype'])){
                return outputJson(4000, '请选择账户性质');
            }
            if($param['accmoney'] == ''){
                return outputJson(4000, '请输入期初金额');
            }
            if(empty($param['accdt'])){
                return outputJson(4000, '请选择期初日期');
            }

            $param['updated_user'] = $this->userinfo['id'];
            $param['updated_at'] = date('Y-m-d H:i:s');
            $files = array_filter($param['files']);
            unset($param['files']);
            unset($param['token']);

            Db::startTrans();
            try {
                AccountModel::update($param);
                (new OptService())->relation_file($files,'account',$param['id']);
                (new OptService())->add_flow_log(FlowLogModel::SET_EDIT, 'account', $param['id']);
                (new OptService())->add_log_list(FlowLogModel::SET_EDIT, 'account', $param['id'], '资金账户');
                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,lang('code_4000'));
            }
            return outputJson(2000,lang('code_2000'));
        }else{
            $id = $request->get('id',0,'int');
            $data['data'] = AccountModel::where(['id'=>$id])->find();
            if(!$data['data']){
                return outputJson(4000, lang('code_4000'));
            }

            $engininfo = (new EnginService())->get_engin_showinfo($data['data']['enginid']);
            $data['engininfo'] = $engininfo;

            $data['data']['chanid_info'] = (new UserService())->get_users_part_arr($data['data']['chanid'], 'dept');
            $data['acctype'] = (new OptService())->get_base_info('cai_account_acctype');
            $data['files'] = (new OptService())->get_file('account', $data['data']['id']);
            return outputJson(2000,lang('code_2000'),$data);
        }
    }
    public function del_account(Request $request){
        $param = $request->post();
        $data = AccountModel::get_info_by_pk($param['id']);
        if(!$data){
            return outputJson(4000,lang('code_4000'));
        }

        Db::startTrans();
        try {
            AccountModel::where(['id'=>$data['id']])->update([
                'deleted_user' => $this->userinfo['id'],
                'deleted_at' => date('Y-m-d H:i:s')
            ]);

            (new OptService())->add_log_list(FlowLogModel::SET_DELETE, 'account', $data['id'], '资金账户');
            Db::commit();
        } catch (\Exception $e) {
            Db::rollback();
            return outputJson(4000,lang('code_4000'));
        }
        return outputJson(2000,lang('code_2000'));
    }
    public function show_account(Request $request){
        $id = $request->post('id', 0, 'int');
        $data['data'] = AccountModel::get_info_by_pk($id);
        if(!$data['data']){
            return outputJson(4000, lang('code_4000'));
        }

        $engininfo = (new EnginService())->get_engin_showinfo($data['data']['enginid']);
        $data['engininfo'] = $engininfo;

        $data['data']['status_name'] = $data['data']['status'] > 0 ? '正常' : '停用';
        $data['data']['chanid_name'] = DeptModel::where(['id'=>$data['data']['chanid']])->value('name');
        $data['data']['acctype'] = (new OptService())->get_base_showname('cai_account_acctype', $data['data']['acctype']);
        $data['files'] = (new OptService())->get_file('account', $data['data']['id']);
        return outputJson(2000,lang('code_2000'), $data);
    }

    public function list_account_shou(Request $request){
        $param = $request->post();
        get_page($param);

        $where[] = ['list.comid','=',$this->userinfo['comid']];
        $where[] = ['acc.deleted_user','=',0];
        if(isset($param['type']) && !empty($param['type'])){
            $where[] = ['list.type','=', $param['type']];
        }else{
            if(isset($param['typearr']) && is_array($param['typearr']) && count($param['typearr']) > 0){
                $where[] = ['list.type','in',$param['typearr']];
            }else{
                $where[] = ['list.type','in',[1,2,8,9,11,14]];
            }
        }

        if(isset($param['mid']) && !empty($param['mid'])){
            $where[] = ['list.mid','=', $param['mid']];
        }


        if(isset($param['bidid']) && !empty($param['bidid'])){
            $where[] = ['list.bidid','=', $param['bidid']];
        }
        if(isset($param['enginid']) && !empty($param['enginid'])){
            $where[] = ['list.enginid','=', $param['enginid']];
        }
        if(isset($param['budid']) && !empty($param['budid'])){
            $where[] = ['list.budid','=', $param['budid']];
        }

        if(isset($param['jiaonum']) && !empty($param['jiaonum'])){
            $where[] = ['list.jiaonum','like', '%'.$param['jiaonum'].'%'];
        }
        if(isset($param['money']) && !empty($param['money'])){
            $where[] = ['list.money','like', '%'.$param['money'].'%'];
        }
        if(isset($param['zhai']) && !empty($param['zhai'])){
            $where[] = ['list.zhai','like', '%'.$param['zhai'].'%'];
        }
        if(isset($param['faer']) && !empty($param['faer'])){
            $where[] = ['list.faer','like', '%'.$param['faer'].'%'];
        }
        if(isset($param['daotype']) && !empty($param['daotype'])){
            $where[] = ['list.daotype','like', '%'.$param['daotype'].'%'];
        }
        if(isset($param['dt']) && !empty($param['dt'])){
            $where[] = ['list.dt','between', explode(' - ', $param['dt'])];
        }
        if(isset($param['month']) && !empty($param['month'])){
            $seamonth = explode(' - ', $param['month']);
            $firstday = $seamonth[0].'-01';
            $ssfist = $seamonth[1].'-01';
            $lastday = date('Y-m-d', strtotime("$ssfist +1 month -1 day"));
            $where[] = ['list.dt','between', [$firstday, $lastday]];
        }


        if(isset($param['accname']) && !empty($param['accname'])){
            $where[] = ['acc.name','like', '%'.$param['accname'].'%'];
        }
        if(isset($param['baner']) && !empty($param['baner'])){
            $where[] = ['list.baner','like', '%'.$param['baner'].'%'];
        }


        $data['type'] = (new FinanceService())->get_account_list_type();
        $data['is_type'] = isset($param['type']) ? $param['type'] : '';


        if(isset($param['chanid']) && !empty($param['chanid'])){
            $where[] = ['acc.chanid','=', $param['chanid']];
        }
        $data['chanid'] = (new UserService())->get_chaninfo_all();
        $data['is_chanid'] = isset($param['chanid']) ? $param['chanid'] : '';

        $data['data'] = AccountListModel::alias('list')
            ->field('list.*,acc.name as accname,acc.accnum,e.name as enginname,bid.name as bidname')
            ->leftJoin('account acc','acc.id=list.accid')
            ->leftJoin('bid bid','bid.id=list.bidid')
            ->leftJoin('engin e','e.id=list.enginid')
            ->where($where)
            ->where((new MenuService())->get_miss_where('account_shou', 'list'))
            ->order('list.id DESC')
            ->paginate($param['pagesize'],false,['page'=>$param['page']])
            ->each(function ($item,$key){
                if($item['bidid'] > 0){
                    $item['enginname'] = $item['bidname'];
                }

                $item['budname'] = (new BudgetService())->get_budid_quanname($item['budid']);
                $item['budname'] = EnginBudshouModel::where(['id'=>$item['budid']])->value('name');
                $item['accname'] = $item['accname'].'('.$item['accnum'].')';
                $item['midinfo'] = (new FinanceService())->get_account_list_midinfo($item['type'], $item['mid']);

                $item['type_name'] = (new FinanceService())->get_account_list_type_name($item['type']);
                return $item;
            })->toArray();

        return outputJson(2000,lang('code_2000'),$data);
    }
    public function list_account_zhi(Request $request){
        $param = $request->post();
        get_page($param);

        $where[] = ['list.comid','=',$this->userinfo['comid']];
        $where[] = ['acc.deleted_user','=',0];
        if(isset($param['type']) && !empty($param['type'])){
            $where[] = ['list.type','=', $param['type']];
        }else{
            if(isset($param['typearr']) && is_array($param['typearr']) && count($param['typearr']) > 0){
                $where[] = ['list.type','in',$param['typearr']];
            }else{
                $where[] = ['list.type','not in',[1,2,8,9,11,14]];
            }
        }

        if(isset($param['mid']) && !empty($param['mid'])){
            $where[] = ['list.mid','=', $param['mid']];
        }


        if(isset($param['bidid']) && !empty($param['bidid'])){
            $where[] = ['list.bidid','=', $param['bidid']];
        }
        if(isset($param['enginid']) && !empty($param['enginid'])){
            $where[] = ['list.enginid','=', $param['enginid']];
        }
        if(isset($param['budid']) && !empty($param['budid'])){
            $where[] = ['list.budid','=', $param['budid']];
        }

        if(isset($param['jiaonum']) && !empty($param['jiaonum'])){
            $where[] = ['list.jiaonum','like', '%'.$param['jiaonum'].'%'];
        }
        if(isset($param['money']) && !empty($param['money'])){
            $where[] = ['list.money','like', '%'.$param['money'].'%'];
        }
        if(isset($param['zhai']) && !empty($param['zhai'])){
            $where[] = ['list.zhai','like', '%'.$param['zhai'].'%'];
        }
        if(isset($param['faer']) && !empty($param['faer'])){
            $where[] = ['list.faer','like', '%'.$param['faer'].'%'];
        }
        if(isset($param['daotype']) && !empty($param['daotype'])){
            $where[] = ['list.daotype','like', '%'.$param['daotype'].'%'];
        }
        if(isset($param['dt']) && !empty($param['dt'])){
            $where[] = ['list.dt','between', explode(' - ', $param['dt'])];
        }
        if(isset($param['month']) && !empty($param['month'])){
            $seamonth = explode(' - ', $param['month']);
            $firstday = $seamonth[0].'-01';
            $ssfist = $seamonth[1].'-01';
            $lastday = date('Y-m-d', strtotime("$ssfist +1 month -1 day"));
            $where[] = ['list.dt','between', [$firstday, $lastday]];
        }
        if(isset($param['accname']) && !empty($param['accname'])){
            $where[] = ['acc.name','like', '%'.$param['accname'].'%'];
        }
        if(isset($param['baner']) && !empty($param['baner'])){
            $where[] = ['list.baner','like', '%'.$param['baner'].'%'];
        }

        $data['type'] = (new FinanceService())->get_account_list_type();
        $data['is_type'] = isset($param['type']) ? $param['type'] : '';



        if(isset($param['chanid']) && !empty($param['chanid'])){
            $where[] = ['acc.chanid','=', $param['chanid']];
        }
        $data['chanid'] = (new UserService())->get_chaninfo_all();
        $data['is_chanid'] = isset($param['chanid']) ? $param['chanid'] : '';

        if(isset($param['typedan']) && $param['typedan'] != ''){
            if($param['typedan'] == 66){
                $topid = EnginBudsubModel::where(['name'=>'项目费用','pid'=>0])->value('id');
                $sonid = EnginBudsubModel::where(['comid'=>$this->userinfo['comid'],'status'=>1])
                    ->where("id=".$topid." or topid=".$topid)->column('id');
                $where[] = ['list.budid','in', $sonid];
                $where[] = ['list.type','=', 4];
            }else{
                $where[] = ['list.type','=', 3];
                $where[] = ['list.budid','=', $param['typedan']];
            }
        }


        $data['data'] = AccountListModel::alias('list')
            ->field('list.*,acc.name as accname,acc.accnum,e.name as enginname,bid.name as bidname')
            ->leftJoin('account acc','acc.id=list.accid')
            ->leftJoin('bid bid','bid.id=list.bidid')
            ->leftJoin('engin e','e.id=list.enginid')
            ->where($where)
            ->where((new MenuService())->get_miss_where('account_zhi', 'list'))
            ->order('list.id DESC')
            ->paginate($param['pagesize'],false,['page'=>$param['page']])
            ->each(function ($item,$key){
                if($item['bidid'] > 0){
                    $item['enginname'] = $item['bidname'];
                }

                if($item['type'] == 3){
                    $item['budname'] = (new PaymentService())->get_payment_budid_name($item['budid']);
                }else{
                    $item['budname'] = EnginBudsubModel::where(['id'=>$item['budid']])->value('name');
                }


                $item['accname'] = $item['accname'].'('.$item['accnum'].')';

                $item['midinfo'] = (new FinanceService())->get_account_list_midinfo($item['type'], $item['mid']);
                $item['type_name'] = (new FinanceService())->get_account_list_type_name($item['type']);



                return $item;
            })->toArray();

        return outputJson(2000,lang('code_2000'),$data);
    }
    public function show_account_shou(Request $request){
        $id = $request->post('id', 0, 'int');
        $data['data'] = AccountListModel::get_info_by_pk($id);
        if(!$data['data']){
            return outputJson(4000, lang('code_4000'));
        }
        $account = AccountModel::get_info_by_pk($data['data']['accid']);
        $data['account'] = $account;


        if($data['data']['type'] == 1 || $data['data']['type'] == 2){
            $data['data']['budid_name'] = (new BudgetService())->get_budid_quanname($data['data']['budid']);
        }elseif($data['data']['type'] == 3){
            $data['data']['budid_name'] = (new PaymentService())->get_payment_budid_name($data['data']['budid']);
        }elseif($data['data']['type'] == 4) {
            $data['data']['budid_name'] = (new BudgetService())->get_subid_quanname($data['data']['budid']);
        }elseif($data['data']['type'] == 5) {
            $data['data']['budid_name'] = (new BudgetService())->get_subid_quanname($data['data']['budid']);
        }elseif($data['data']['type'] == 6){
            $data['data']['budid_name'] = (new BudgetService())->get_subid_quanname($data['data']['budid']);
        }elseif($data['data']['type'] == 7){
            $data['data']['budid_name'] = (new BudgetService())->get_subid_quanname($data['data']['budid']);
        }elseif($data['data']['type'] == 8){
            $data['data']['budid_name'] = (new BudgetService())->get_budid_quanname($data['data']['budid']);
        }elseif($data['data']['type'] == 9){
            $data['data']['budid_name'] = (new BudgetService())->get_budid_quanname($data['data']['budid']);
        }elseif($data['data']['type'] == 10){
            $data['data']['budid_name'] = (new BudgetService())->get_subid_quanname($data['data']['budid']);
        }elseif($data['data']['type'] == 11){
            $data['data']['budid_name'] = (new BudgetService())->get_budid_quanname($data['data']['budid']);
        }elseif($data['data']['type'] == 12){
            $data['data']['budid_name'] = (new BudgetService())->get_subid_quanname($data['data']['budid']);
        }elseif($data['data']['type'] == 13){
            $data['data']['budid_name'] = (new BudgetService())->get_subid_quanname($data['data']['budid']);
        }elseif($data['data']['type'] == 14){
            $data['data']['budid_name'] = (new BudgetService())->get_budid_quanname($data['data']['budid']);
        }elseif($data['data']['type'] == 15) {
            $data['data']['budid_name'] = (new BudgetService())->get_subid_quanname($data['data']['budid']);
        }

        $data['midinfo'] = (new FinanceService())->get_account_list_midinfo($data['data']['type'], $data['data']['mid']);

        $data['data']['type_name'] = (new FinanceService())->get_account_list_type_name($data['data']['type']);

        $data['files'] = (new OptService())->get_file('account_list', $data['data']['id']);
        return outputJson(2000,lang('code_2000'), $data);
    }


    public function list_account_diao(Request $request){
        $param = $request->post();
        get_page($param);

        $where[] = ['diao.deleted_user','=', 0];
        $where[] = ['diao.comid','=',$this->userinfo['comid']];
        $where[] = ['diao.isturn','=',1];
        $where[] = ['diao.status','in',[0,1]];

        if(isset($param['keyword']) && !empty($param['keyword'])){
            $where[] = ['diao.zhai|chu.name|ru.name|diao.baner','like', '%'.$param['keyword'].'%'];
        }

        if(isset($param['zhai']) && !empty($param['zhai'])){
            $where[] = ['diao.zhai','like', '%'.$param['zhai'].'%'];
        }
        if(isset($param['chuname']) && !empty($param['chuname'])){
            $where[] = ['chu.name','like', '%'.$param['chuname'].'%'];
        }
        if(isset($param['runame']) && !empty($param['runame'])){
            $where[] = ['ru.name','like', '%'.$param['runame'].'%'];
        }
        if(isset($param['dt']) && !empty($param['dt'])){
            $where[] = ['diao.dt','between', explode(' - ', $param['dt'])];
        }
        if(isset($param['baner']) && !empty($param['baner'])){
            $where[] = ['diao.baner','like', '%'.$param['baner'].'%'];
        }
        if(isset($param['status']) && $param['status'] != ''){
            $where[] = ['diao.status','=', $param['status']];
        }

        $data['data'] = AccountDiaoModel::alias('diao')
            ->field('diao.*,chu.name as chuname,chu.accnum as chunum,ru.name as runame,ru.accnum as runum')
            ->leftJoin('account chu','chu.id=diao.chuid')
            ->leftJoin('account ru','ru.id=diao.ruid')
            ->where($where)
            ->where((new MenuService())->get_miss_where('account_diao', 'diao'))
            ->order('diao.id DESC')
            ->paginate($param['pagesize'],false,['page'=>$param['page']])
            ->each(function ($item,$key){
                $item['chuname'] = $item['chuname'].'('.$item['chunum'].')';
                $item['runame'] = $item['runame'].'('.$item['runum'].')';

                $item['zhiqilei_name'] = (new OptService())->get_base_showname('cai_zhiqilei', $item['zhiqilei']);
                $item['shouqilei_name'] = (new OptService())->get_base_showname('cai_shouqilei', $item['shouqilei']);
                return $item;
            })->toArray();

        $data['butauth'] = (new MenuService())->get_butauth('account_diao');
        return outputJson(2000,lang('code_2000'),$data);
    }
    public function add_account_diao(Request $request){
        if($request->isPost()){
            $param = $request->post();

            if(empty($param['zhai'])){
                return outputJson(4000, '请输入款项摘要');
            }
            if(empty($param['chuid'])){
                return outputJson(4000, '请选择调出(付款)账户');
            }
            if(empty($param['chubudid'])){
                return outputJson(4000, '请选择款项科目(支出)');
            }
            if(empty($param['ruid'])){
                return outputJson(4000, '请选择调入(收款)账户');
            }
            if(empty($param['rubudid'])){
                return outputJson(4000, '请选择款项科目(收入)');
            }

            if($param['chuid'] == $param['chubudid']){
                return outputJson(4000, '调出账户不能与调入账户相同');
            }
            if(empty($param['money'])){
                return outputJson(4000, '请输入转账金额');
            }
            if(empty($param['dt'])){
                return outputJson(4000, '请选择交易日期');
            }
            if(empty($param['baner'])){
                return outputJson(4000, '请输入经手人');
            }

            $param['number'] = (new FinanceService())->create_account_diao_number();
            $param['status'] = 1;
            $param['isturn'] = 1;
            $param['comid'] = $this->userinfo['comid'];
            $param['created_user'] = $param['updated_user'] = $this->userinfo['id'];
            $param['created_at'] = $param['updated_at']= date('Y-m-d H:i:s');
            $param['deptid'] = $this->userinfo['deptid'];
            $param['chanid'] = (new UserService())->get_user_chanid();

            $files = array_filter($param['files']);
            unset($param['files']);
            unset($param['token']);


            Db::startTrans();
            try {
                $id = AccountDiaoModel::insertGetId($param);
                (new FinanceService())->create_account_diao_shou($id, $param);
                (new FinanceService())->create_account_diao_zhi($id, $param);
                (new OptService())->relation_file($files,'account_diao',$id);
                (new OptService())->add_flow_log(FlowLogModel::SET_ADD, 'account_diao', $id);
                (new OptService())->add_log_list(FlowLogModel::SET_ADD, 'account_diao', $id, '资金调拨');
                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,$e->getMessage());
                return outputJson(4000,lang('code_4000'));
            }
            return outputJson(2000,lang('code_2000'));
        }else{
            $data['dt'] = date("Y-m-d");
            $data['baner'] = $this->userinfo['realname'];
            $data['number'] = (new FinanceService())->create_account_diao_number();
            $data['subchuarr'] = (new BudgetService())->get_engin_budsub_guan_gong_all_xiala();
            $data['subruarr'] = (new BudgetService())->get_engin_budshou_qi_all_xiala();
            $data['files'] = (new OptService())->get_file('account_diao');
            return outputJson(2000,lang('code_2000'), $data);
        }
    }
    public function edit_account_diao(Request $request){
        if($request->isPost()){
            $param = $request->post();

            if(empty($param['zhai'])){
                return outputJson(4000, '请输入款项摘要');
            }
            if(empty($param['chubudid'])){
                return outputJson(4000, '请选择款项科目(支出)');
            }
            if(empty($param['rubudid'])){
                return outputJson(4000, '请选择款项科目(收入)');
            }
            if(empty($param['money'])){
                return outputJson(4000, '请输入转账金额');
            }
            if(empty($param['dt'])){
                return outputJson(4000, '请选择交易日期');
            }
            if(empty($param['baner'])){
                return outputJson(4000, '请输入经手人');
            }

            $param['updated_user'] = $this->userinfo['id'];
            $param['updated_at'] = date('Y-m-d H:i:s');
            $files = array_filter($param['files']);
            unset($param['files']);
            unset($param['token']);

            Db::startTrans();
            try {
                AccountDiaoModel::update($param);

                (new FinanceService())->update_account_diao_shou($param['id'], $param);
                (new FinanceService())->update_account_diao_zhi($param['id'], $param);

                (new OptService())->relation_file($files,'account_diao',$param['id']);
                (new OptService())->add_flow_log(FlowLogModel::SET_EDIT, 'account_diao', $param['id']);
                (new OptService())->add_log_list(FlowLogModel::SET_EDIT, 'account_diao', $param['id'], '资金调拨');
                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,lang('code_4000'));
            }
            return outputJson(2000,lang('code_2000'));
        }else{
            $id = $request->get('id',0,'int');
            $data['data'] = AccountDiaoModel::where(['id'=>$id])->find();
            if(!$data['data']){
                return outputJson(4000, lang('code_4000'));
            }

            $chuaccount = AccountModel::get_info_by_pk($data['data']['chuid']);
            $data['chuaccount'] = $chuaccount;

            $ruaccount = AccountModel::get_info_by_pk($data['data']['ruid']);
            $data['ruaccount'] = $ruaccount;

            $chubudinfo = EnginBudsubModel::where(['id'=>$data['data']['chubudid']])->find();
            $data['chubudinfo'] = $chubudinfo;

            $rubudinfo = EnginBudshouModel::where(['id'=>$data['data']['rubudid']])->find();
            $data['rubudinfo'] = $rubudinfo;
            $data['subchuarr'] = (new BudgetService())->get_engin_budsub_guan_gong_all_xiala();
            $data['subruarr'] = (new BudgetService())->get_engin_budshou_qi_all_xiala();
            $data['files'] = (new OptService())->get_file('account_diao', $data['data']['id']);
            return outputJson(2000,lang('code_2000'),$data);
        }
    }
    public function del_account_diao(Request $request){
        $param = $request->post();
        $data = AccountDiaoModel::get_info_by_pk($param['id']);
        if(!$data){
            return outputJson(4000,lang('code_4000'));
        }

        Db::startTrans();
        try {
            AccountDiaoModel::where(['id'=>$data['id']])->update([
                'deleted_user' => $this->userinfo['id'],
                'deleted_at' => date('Y-m-d H:i:s')
            ]);

            (new FinanceService())->del_account_diao_shou($data['id']);
            (new FinanceService())->del_account_diao_zhi($data['id']);

            (new OptService())->add_log_list(FlowLogModel::SET_DELETE, 'account_diao', $data['id'], '资金调拨');
            Db::commit();
        } catch (\Exception $e) {
            Db::rollback();
            return outputJson(4000,lang('code_4000'));
        }
        return outputJson(2000,lang('code_2000'));
    }
    public function show_account_diao(Request $request){
        $id = $request->post('id', 0, 'int');
        $data['data'] = AccountDiaoModel::get_info_by_pk($id);
        if(!$data['data']){
            return outputJson(4000, lang('code_4000'));
        }

        $chuaccount = AccountModel::get_info_by_pk($data['data']['chuid']);
        $data['chuaccount'] = $chuaccount;

        $ruaccount = AccountModel::get_info_by_pk($data['data']['ruid']);
        $data['ruaccount'] = $ruaccount;

        $data['data']['zhiqilei_name'] = (new OptService())->get_base_showname('cai_zhiqilei', $data['data']['zhiqilei']);
        $data['data']['shouqilei_name'] = (new OptService())->get_base_showname('cai_shouqilei', $data['data']['shouqilei']);

        $data['files'] = (new OptService())->get_file('account_diao', $data['data']['id']);
        $data['kuaninfo'] = (new FinanceService())->get_acckuan_info_bybillid($data['data']['billid']);
        $data['billinfo'] = (new FlowService())->get_flow_show_info($data['data']['billid']);
        return outputJson(2000,lang('code_2000'), $data);
    }


    public function list_engin_chu(Request $request){
        $param = $request->post();
        get_page($param);

        $where[] = ['chu.deleted_user','=', 0];
        $where[] = ['chu.comid','=',$this->userinfo['comid']];


        if(isset($param['enginname']) && !empty($param['enginname'])){
            $where[] = ['e.name','like', '%'.$param['enginname'].'%'];
        }
        if(isset($param['enginnumber']) && !empty($param['enginnumber'])){
            $where[] = ['e.number','like', '%'.$param['enginnumber'].'%'];
        }
        if(isset($param['shoufee']) && !empty($param['shoufee'])){
            $where[] = ['chu.shoufee','like', '%'.$param['shoufee'].'%'];
        }
        if(isset($param['zhifee']) && !empty($param['zhifee'])){
            $where[] = ['chu.zhifee','like', '%'.$param['zhifee'].'%'];
        }
        if(isset($param['koufee']) && !empty($param['koufee'])){
            $where[] = ['chu.koufee','like', '%'.$param['koufee'].'%'];
        }
        if(isset($param['money']) && !empty($param['money'])){
            $where[] = ['chu.money','like', '%'.$param['money'].'%'];
        }
        if(isset($param['dt']) && !empty($param['dt'])){
            $where[] = ['chu.dt','between', explode(' - ', $param['dt'])];
        }
        if(isset($param['chanid']) && !empty($param['chanid'])){
            $where[] = ['e.chanid','=', $param['chanid']];
        }
        if(isset($param['business']) && !empty($param['business'])){
            $where[] = ['e.business','=', $param['business']];
        }
        $data['chanid'] = (new UserService())->get_chaninfo_all();;
        $data['is_chanid'] = isset($param['chanid']) ? $param['chanid'] : '';


        $data['data'] = EnginChuModel::alias('chu')
            ->field('chu.*,e.name as enginname')
            ->join('engin e','e.id=chu.enginid')
            ->where($where)
            ->where((new MenuService())->get_miss_where('engin_chu', 'chu'))
            ->order('chu.id DESC')
            ->paginate($param['pagesize'],false,['page'=>$param['page']])
            ->each(function ($item,$key){

                return $item;
            })->toArray();

        $data['butauth'] = (new MenuService())->get_butauth('engin_chu');
        return outputJson(2000,lang('code_2000'),$data);
    }
    public function add_engin_chu(Request $request){
        if($request->isPost()){
            $param = $request->post();

            if(empty($param['enginid'])){
                return outputJson(4000, '请选择工程名称');
            }
            if($param['shoufee'] == ''){
                return outputJson(4000, '请输入收款期初');
            }
            if($param['zhifee'] == ''){
                return outputJson(4000, '请输入付款期初');
            }
            if($param['money'] == ''){
                return outputJson(4000, '请输入余额期初');
            }
            if(empty($param['dt'])){
                return outputJson(4000, '请选择期初日期');
            }

            $param['comid'] = $this->userinfo['comid'];
            $param['created_user'] = $param['updated_user'] = $this->userinfo['id'];
            $param['created_at'] = $param['updated_at']= date('Y-m-d H:i:s');
            $param['deptid'] = $this->userinfo['deptid'];
            $param['chanid'] = (new UserService())->get_user_chanid();

            $files = array_filter($param['files']);
            unset($param['files']);
            unset($param['token']);


            Db::startTrans();
            try {
                $id = EnginChuModel::insertGetId($param);
                (new OptService())->relation_file($files,'engin_chu',$id);
                (new OptService())->add_flow_log(FlowLogModel::SET_ADD, 'engin_chu', $id);
                (new OptService())->add_log_list(FlowLogModel::SET_ADD, 'engin_chu', $id, '项目期初');
                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,lang('code_4000'));
            }
            return outputJson(2000,lang('code_2000'));
        }else{
            $data['files'] = (new OptService())->get_file('engin_chu');
            return outputJson(2000,lang('code_2000'), $data);
        }
    }
    public function edit_engin_chu(Request $request){
        if($request->isPost()){
            $param = $request->post();


            if($param['shoufee'] == ''){
                return outputJson(4000, '请输入收款期初');
            }
            if($param['zhifee'] == ''){
                return outputJson(4000, '请输入付款期初');
            }
            if($param['money'] == ''){
                return outputJson(4000, '请输入余额期初');
            }
            if(empty($param['dt'])){
                return outputJson(4000, '请选择期初日期');
            }

            $param['updated_user'] = $this->userinfo['id'];
            $param['updated_at'] = date('Y-m-d H:i:s');
            $files = array_filter($param['files']);
            unset($param['files']);
            unset($param['token']);

            Db::startTrans();
            try {
                EnginChuModel::update($param);
                (new OptService())->relation_file($files,'engin_chu',$param['id']);
                (new OptService())->add_flow_log(FlowLogModel::SET_EDIT, 'engin_chu', $param['id']);
                (new OptService())->add_log_list(FlowLogModel::SET_EDIT, 'engin_chu', $param['id'], '项目期初');
                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,lang('code_4000'));
            }
            return outputJson(2000,lang('code_2000'));
        }else{
            $id = $request->get('id',0,'int');
            $data['data'] = EnginChuModel::where(['id'=>$id])->find();
            if(!$data['data']){
                return outputJson(4000, lang('code_4000'));
            }

            $engininfo = EnginModel::get_info_by_pk($data['data']['enginid']);
            $data['engininfo'] = $engininfo;


            $data['files'] = (new OptService())->get_file('engin_chu', $data['data']['id']);
            return outputJson(2000,lang('code_2000'),$data);
        }
    }
    public function del_engin_chu(Request $request){
        $param = $request->post();
        $data = EnginChuModel::get_info_by_pk($param['id']);
        if(!$data){
            return outputJson(4000,lang('code_4000'));
        }

        Db::startTrans();
        try {
            EnginChuModel::where(['id'=>$data['id']])->update([
                'deleted_user' => $this->userinfo['id'],
                'deleted_at' => date('Y-m-d H:i:s')
            ]);

            (new OptService())->add_log_list(FlowLogModel::SET_DELETE, 'engin_chu', $data['id'], '项目期初');
            Db::commit();
        } catch (\Exception $e) {
            Db::rollback();
            return outputJson(4000,lang('code_4000'));
        }
        return outputJson(2000,lang('code_2000'));
    }
    public function show_engin_chu(Request $request){
        $id = $request->post('id', 0, 'int');
        $data['data'] = EnginChuModel::get_info_by_pk($id);
        if(!$data['data']){
            return outputJson(4000, lang('code_4000'));
        }
        $engininfo = EnginModel::get_info_by_pk($data['data']['enginid']);
        $data['engininfo'] = $engininfo;
        $data['files'] = (new OptService())->get_file('engin_chu', $data['data']['id']);
        return outputJson(2000,lang('code_2000'), $data);
    }





    public function list_kuan_shou(Request $request){
        $param = $request->post();
        get_page($param);

        $where[] = ['kuan.deleted_user','=', 0];
        $where[] = ['kuan.comid','=',$this->userinfo['comid']];
        $where[] = ['kuan.modtype','<',10];

        if(isset($param['keyword']) && !empty($param['keyword'])){
            $where[] = ['bill.sericnum|kuan.zhai|kuai.kuanlei|kuan.duier|kuan.incdaotype','like', '%'.$param['keyword'].'%'];
        }
        if(isset($param['modtype']) && !empty($param['modtype'])){
            $where[] = ['kuan.modtype','=', $param['modtype']];
        }
        if(isset($param['sericnum']) && !empty($param['sericnum'])){
            $where[] = ['bill.sericnum','like', '%'.$param['sericnum'].'%'];
        }
        if(isset($param['zhai']) && !empty($param['zhai'])){
            $where[] = ['kuan.zhai','like', '%'.$param['zhai'].'%'];
        }
        if(isset($param['kuanlei']) && !empty($param['kuanlei'])){
            $where[] = ['kuan.kuanlei','like', '%'.$param['kuanlei'].'%'];
        }
        if(isset($param['duier']) && !empty($param['duier'])){
            $where[] = ['kuan.duier','like', '%'.$param['duier'].'%'];
        }
        if(isset($param['dt']) && !empty($param['dt'])){
            $where[] = ['kuan.dt','between', explode(' - ', $param['dt'])];
        }
        if(isset($param['incdaotype']) && !empty($param['incdaotype'])){
            $where[] = ['kuan.incdaotype','like', '%'.$param['incdaotype'].'%'];
        }

        $whereEngin = "";
        if(isset($param['enginname']) && !empty($param['enginname'])){
            $whereEngin = "b.name like '%".$param['enginname']."%' or e.name like '%".$param['enginname']."%'";
        }

        $data['modtype'] = (new KuanService())->get_acckuan_modtype(1);
        $data['is_modtype'] = isset($param['modtype']) ? $param['modtype'] : '';

        $data['data'] = AcckuanModel::alias('kuan')
            ->field('kuan.*,b.name as bidname,e.name as enginname,acc.name as accname,acc.accnum,
            bill.sericnum,bill.status,bill.modenum,bill.mid')
            ->leftJoin('bid b','b.id=kuan.bidid')
            ->leftJoin('engin e','e.id=kuan.enginid')
            ->leftJoin('account acc','acc.id=kuan.accid')
            ->join('flow_bill bill','bill.id=kuan.billid')
            ->where($where)
            ->where($whereEngin)
            ->order('kuan.id DESC')
            ->paginate($param['pagesize'],false,['page'=>$param['page']])
            ->each(function ($item,$key){
                if($item['bidid'] > 0){
                    $item['enginname'] = $item['bidname'];
                }
                $item['modtype_name'] = (new KuanService())->get_acckuan_modtype_name($item['modtype']);
                $item['accid_name'] = $item['accname'].'('.$item['accnum'].')';

                return $item;
            })->toArray();

        return outputJson(2000,lang('code_2000'),$data);
    }
    public function list_kuan_zhi(Request $request){
        $param = $request->post();
        get_page($param);

        $where = [];
        $whereZhu[] = ['kuan.deleted_user','=', 0];
        $whereZhu[] = ['kuan.comid','=',$this->userinfo['comid']];
        $whereZhu[] = ['kuan.modtype','>',10];

        if(isset($param['keyword']) && !empty($param['keyword'])){
            $where[] = ['bill.sericnum|kuan.zhai|kuai.kuanlei|kuan.duier|kuan.incdaotype','like', '%'.$param['keyword'].'%'];
        }
        if(isset($param['modtype']) && !empty($param['modtype'])){
            $where[] = ['kuan.modtype','=', $param['modtype']];
        }
        if(isset($param['sericnum']) && !empty($param['sericnum'])){
            $where[] = ['bill.sericnum','like', '%'.$param['sericnum'].'%'];
        }
        if(isset($param['zhai']) && !empty($param['zhai'])){
            $where[] = ['kuan.zhai','like', '%'.$param['zhai'].'%'];
        }
        if(isset($param['kuanlei']) && !empty($param['kuanlei'])){
            $where[] = ['kuan.kuanlei','like', '%'.$param['kuanlei'].'%'];
        }
        if(isset($param['duier']) && !empty($param['duier'])){
            $where[] = ['kuan.duier','like', '%'.$param['duier'].'%'];
        }
        if(isset($param['dt']) && !empty($param['dt'])){
            $where[] = ['kuan.dt','between', explode(' - ', $param['dt'])];
        }
        if(isset($param['incdaotype']) && !empty($param['incdaotype'])){
            $where[] = ['kuan.incdaotype','like', '%'.$param['incdaotype'].'%'];
        }

        $whereEngin = "";
        if(isset($param['enginname']) && !empty($param['enginname'])){
            $whereEngin = "b.name like '%".$param['enginname']."%' or e.name like '%".$param['enginname']."%'";
        }

        $data['modtype'] = (new KuanService())->get_acckuan_modtype(2);
        $data['is_modtype'] = isset($param['modtype']) ? $param['modtype'] : '';



        $wherePayss = [];
        if(isset($param['payss']) && !empty($param['payss'])){
            if($param['payss'] == 'no'){
                $wherePayss[] = ['kuan.payfee','=', 0];
            }
            if($param['payss'] == 'bu'){
                $wherePayss[] = ['kuan.payfee','neq', 0];
                $wherePayss[] = Db::raw('kuan.payfee < kuan.money');
            }
            if($param['payss'] == 'quan'){
                $wherePayss[] = ['kuan.payfee','neq', 0];
                $wherePayss[] = Db::raw('kuan.payfee >= kuan.money');
            }
        }
        $leftarr = [];
        $leftarr['no'] = AcckuanModel::alias('kuan')
            ->field('kuan.*,b.name as bidname,e.name as enginname,
            bill.sericnum,bill.status,bill.modenum,bill.mid')
            ->leftJoin('bid b','b.id=kuan.bidid')
            ->leftJoin('engin e','e.id=kuan.enginid')
            ->join('flow_bill bill','bill.id=kuan.billid')
            ->where($where)
            ->where($whereZhu)
            ->where($whereEngin)
            ->where('kuan.payfee','=',0)
            ->count();
        $leftarr['bu'] = AcckuanModel::alias('kuan')
            ->field('kuan.*,b.name as bidname,e.name as enginname,
            bill.sericnum,bill.status,bill.modenum,bill.mid')
            ->leftJoin('bid b','b.id=kuan.bidid')
            ->leftJoin('engin e','e.id=kuan.enginid')
            ->join('flow_bill bill','bill.id=kuan.billid')
            ->where($where)
            ->where($whereZhu)
            ->where($whereEngin)
            ->where('kuan.payfee < kuan.money')
            ->where('kuan.payfee','neq',0)
            ->count();
        $leftarr['quan'] = AcckuanModel::alias('kuan')
            ->field('kuan.*,b.name as bidname,e.name as enginname,
            bill.sericnum,bill.status,bill.modenum,bill.mid')
            ->leftJoin('bid b','b.id=kuan.bidid')
            ->leftJoin('engin e','e.id=kuan.enginid')
            ->join('flow_bill bill','bill.id=kuan.billid')
            ->where($where)
            ->where($whereZhu)
            ->where($whereEngin)
            ->where('kuan.payfee >= kuan.money')
            ->where('kuan.payfee','neq',0)
            ->count();
        $leftarr['all'] = $leftarr['no'] + $leftarr['bu'] + $leftarr['quan'];
        $data['leftarr'] = $leftarr;



        $data['data'] = AcckuanModel::alias('kuan')
            ->field('kuan.*,b.name as bidname,e.name as enginname,
            bill.sericnum,bill.status,bill.modenum,bill.mid')
            ->leftJoin('bid b','b.id=kuan.bidid')
            ->leftJoin('engin e','e.id=kuan.enginid')
            ->join('flow_bill bill','bill.id=kuan.billid')
            ->where($where)
            ->where($wherePayss)
            ->where($whereZhu)
            ->where($whereEngin)
            ->order('kuan.id DESC')
            ->paginate($param['pagesize'],false,['page'=>$param['page']])
            ->each(function ($item,$key){
                if($item['bidid'] > 0){
                    $item['enginname'] = $item['bidname'];
                }
                $item['modtype_name'] = (new KuanService())->get_acckuan_modtype_name($item['modtype']);
                $item['paystate_name'] = (new KuanService())->get_acckuan_paystate_name($item['paystate']);

                return $item;
            })->toArray();

        $data['butauth'] = (new MenuService())->get_butauth('kuan_zhi');
        return outputJson(2000,lang('code_2000'),$data);
    }

    public function list_account_list(Request $request){
        $param = $request->post();
        get_page($param);

        $where[] = ['list.comid','=',$this->userinfo['comid']];
        if(isset($param['type']) && $param['type'] == 1){
            $where[] = ['kuan.modtype','<', 10];
        }
        if(isset($param['type']) && $param['type'] == 2){
            $where[] = ['kuan.modtype','>', 10];
        }
        if(isset($param['kuanid']) && !empty($param['kuanid'])){
            $where[] = ['list.kuanid','=', $param['kuanid']];
        }
        if(isset($param['accid']) && !empty($param['accid'])){
            $where[] = ['list.accid','=', $param['accid']];
        }

        if(isset($param['keyword']) && !empty($param['keyword'])){
            $where[] = ['bill.sericnum|kuan.zhai|kuai.kuanlei|kuan.duier|kuan.incdaotype|list.jiaonum|list.baner|list.money|acc.name','like', '%'.$param['keyword'].'%'];
        }
        if(isset($param['modtype']) && !empty($param['modtype'])){
            $where[] = ['kuan.modtype','=', $param['modtype']];
        }
        if(isset($param['sericnum']) && !empty($param['sericnum'])){
            $where[] = ['bill.sericnum','like', '%'.$param['sericnum'].'%'];
        }
        if(isset($param['enginname']) && !empty($param['enginname'])){
            $where[] = ['e.name|b.name','like', '%'.$param['enginname'].'%'];
        }
        if(isset($param['zhai']) && !empty($param['zhai'])){
            $where[] = ['kuan.zhai','like', '%'.$param['zhai'].'%'];
        }
        if(isset($param['kuanlei']) && !empty($param['kuanlei'])){
            $where[] = ['kuan.kuanlei','like', '%'.$param['kuanlei'].'%'];
        }
        if(isset($param['duier']) && !empty($param['duier'])){
            $where[] = ['kuan.duier','like', '%'.$param['duier'].'%'];
        }
        if(isset($param['dt']) && !empty($param['dt'])){
            $where[] = ['kuan.dt','between', explode(' - ', $param['dt'])];
        }
        if(isset($param['incdaotype']) && !empty($param['incdaotype'])){
            $where[] = ['kuan.incdaotype','like', '%'.$param['incdaotype'].'%'];
        }
        if(isset($param['jiaonum']) && !empty($param['jiaonum'])){
            $where[] = ['list.jiaonum','like', '%'.$param['jiaonum'].'%'];
        }
        if(isset($param['money']) && !empty($param['money'])){
            $where[] = ['list.money','like', '%'.$param['money'].'%'];
        }
        if(isset($param['accname']) && !empty($param['accname'])){
            $where[] = ['acc.name','like', '%'.$param['accname'].'%'];
        }
        if(isset($param['baner']) && !empty($param['baner'])){
            $where[] = ['list.baner','like', '%'.$param['baner'].'%'];
        }

        if(isset($param['acc_name']) && !empty($param['acc_name'])){
            $where[] = ['acc.name','like', '%'.$param['acc_name'].'%'];
        }
        if(isset($param['acc_accname']) && !empty($param['acc_accname'])){
            $where[] = ['acc.accname','like', '%'.$param['acc_accname'].'%'];
        }
        if(isset($param['acc_accnum']) && !empty($param['acc_accnum'])){
            $where[] = ['acc.accnum','like', '%'.$param['acc_accnum'].'%'];
        }
        if(isset($param['acctype']) && !empty($param['acctype'])){
            $where[] = ['acc.acctype','=', $param['acctype']];
        }


        $whereEngin = "";
        if(isset($param['enginname']) && !empty($param['enginname'])){
            $whereEngin = "b.name like '%".$param['enginname']."%' or e.name like '%".$param['enginname']."%'";
        }

        $data['modtype'] = (new KuanService())->get_acckuan_modtype(0);
        $data['is_modtype'] = isset($param['modtype']) ? $param['modtype'] : '';

        $data['acctype'] = (new OptService())->get_base_info('cai_account_acctype');
        $data['is_acctype'] = isset($param['acctype']) ? $param['acctype'] : '';



        $data['data'] = AccountListModel::alias('list')
            ->field('list.*,b.name as bidname,e.name as enginname,
            bill.sericnum,bill.status,bill.modenum,bill.mid,kuan.listid,
            kuan.modtype,kuan.bidid,kuan.enginid,kuan.zhai,kuan.kuanlei,kuan.duier,kuan.incdaotype
            ,acc.name as acc_name,acc.accname as acc_accname,acc.acctype as acctype,acc.accnum as acc_accnum')
            ->join('acckuan kuan','kuan.id=list.kuanid')
            ->leftJoin('bid b','b.id=kuan.bidid')
            ->leftJoin('engin e','e.id=kuan.enginid')
            ->leftJoin('account acc','acc.id=list.accid')
            ->join('flow_bill bill','bill.id=kuan.billid')
            ->where($where)
            ->where($whereEngin)
            ->order('list.id DESC')
            ->paginate($param['pagesize'],false,['page'=>$param['page']])
            ->each(function ($item,$key){
                $item['type_name'] = $item['modtype'] > 10 ? '支出' : '收入';
                if($item['bidid'] > 0){
                    $item['enginname'] = $item['bidname'];
                }
                $item['modtype_name'] = (new KuanService())->get_acckuan_modtype_name($item['modtype']);
                $item['acc_acctype'] = (new OptService())->get_base_showname('cai_account_acctype', $item['acctype']);
                return $item;
            })->toArray();
        $data['butauth'] = (new MenuService())->get_butauth('account_list');
        return outputJson(2000,lang('code_2000'),$data);
    }
    public function list_account_list_zhipiao(Request $request){
        $param = $request->post();
        get_page($param);

        $where[] = ['list.comid','=',$this->userinfo['comid']];
        $where[] = ['list.modtype','in',[11,12,16,19]];

        if(isset($param['accid']) && !empty($param['accid'])){
            $where[] = ['list.accid','=', $param['accid']];
        }

        if(isset($param['keyword']) && !empty($param['keyword'])){
            $where[] = ['bill.sericnum|kuan.zhai|kuai.kuanlei|kuan.duier|kuan.incdaotype|list.jiaonum|list.baner|list.money|acc.name','like', '%'.$param['keyword'].'%'];
        }
        if(isset($param['modtype']) && !empty($param['modtype'])){
            $where[] = ['kuan.modtype','=', $param['modtype']];
        }
        if(isset($param['sericnum']) && !empty($param['sericnum'])){
            $where[] = ['bill.sericnum','like', '%'.$param['sericnum'].'%'];
        }
        if(isset($param['zhai']) && !empty($param['zhai'])){
            $where[] = ['kuan.zhai','like', '%'.$param['zhai'].'%'];
        }
        if(isset($param['kuanlei']) && !empty($param['kuanlei'])){
            $where[] = ['kuan.kuanlei','like', '%'.$param['kuanlei'].'%'];
        }
        if(isset($param['duier']) && !empty($param['duier'])){
            $where[] = ['kuan.duier','like', '%'.$param['duier'].'%'];
        }
        if(isset($param['dt']) && !empty($param['dt'])){
            $where[] = ['kuan.dt','between', explode(' - ', $param['dt'])];
        }
        if(isset($param['incdaotype']) && !empty($param['incdaotype'])){
            $where[] = ['kuan.incdaotype','like', '%'.$param['incdaotype'].'%'];
        }
        if(isset($param['jiaonum']) && !empty($param['jiaonum'])){
            $where[] = ['list.jiaonum','like', '%'.$param['jiaonum'].'%'];
        }
        if(isset($param['money']) && !empty($param['money'])){
            $where[] = ['list.money','like', '%'.$param['money'].'%'];
        }
        if(isset($param['accname']) && !empty($param['accname'])){
            $where[] = ['acc.name','like', '%'.$param['accname'].'%'];
        }
        if(isset($param['baner']) && !empty($param['baner'])){
            $where[] = ['list.baner','like', '%'.$param['baner'].'%'];
        }

        if(isset($param['acc_name']) && !empty($param['acc_name'])){
            $where[] = ['acc.name','like', '%'.$param['acc_name'].'%'];
        }
        if(isset($param['acc_accname']) && !empty($param['acc_accname'])){
            $where[] = ['acc.accname','like', '%'.$param['acc_accname'].'%'];
        }
        if(isset($param['acc_accnum']) && !empty($param['acc_accnum'])){
            $where[] = ['acc.accnum','like', '%'.$param['acc_accnum'].'%'];
        }
        if(isset($param['acctype']) && !empty($param['acctype'])){
            $where[] = ['acc.acctype','=', $param['acctype']];
        }

        $data['acctype'] = (new OptService())->get_base_info('cai_account_acctype');
        $data['is_acctype'] = isset($param['acctype']) ? $param['acctype'] : '';

        $whereEngin = "";
        if(isset($param['enginname']) && !empty($param['enginname'])){
            $whereEngin = "b.name like '%".$param['enginname']."%' or e.name like '%".$param['enginname']."%'";
        }


        $data['modtype'] = [
            11 => '工程付款',
            12 => '前期费支付',
            16 => '储备进项支付',
            19 => '日常付款',
        ];
        $data['is_modtype'] = isset($param['modtype']) ? $param['modtype'] : '';


        $data['data'] = AccountListModel::alias('list')
            ->field('list.*,b.name as bidname,e.name as enginname,
            bill.sericnum,bill.status,bill.modenum,bill.mid,kuan.listid,
            kuan.modtype,kuan.bidid,kuan.enginid,kuan.zhai,kuan.kuanlei,kuan.duier,kuan.incdaotype
            ,acc.name as acc_name,acc.accname as acc_accname,acc.acctype as acctype,acc.accnum as acc_accnum')
            ->join('acckuan kuan','kuan.id=list.kuanid')
            ->leftJoin('bid b','b.id=kuan.bidid')
            ->leftJoin('engin e','e.id=kuan.enginid')
            ->leftJoin('account acc','acc.id=list.accid')
            ->join('flow_bill bill','bill.id=kuan.billid')
            ->where($where)
            ->where($whereEngin)
            ->order('list.id DESC')
            ->paginate($param['pagesize'],false,['page'=>$param['page']])
            ->each(function ($item,$key){
                $item['type_name'] = $item['modtype'] > 10 ? '支出' : '收入';
                if($item['bidid'] > 0){
                    $item['enginname'] = $item['bidname'];
                }
                $item['modtype_name'] = (new KuanService())->get_acckuan_modtype_name($item['modtype']);
                $item['acc_acctype'] = (new OptService())->get_base_showname('cai_account_acctype', $item['acctype']);



                $item['statename'] = '';

                // 工程支付【11】
                if($item['modtype'] == 11){
                    $listInfo = HouGongzhiListModel::where(['id'=>$item['listid']])->find();
                    $item['yifufee'] = HouGongzhiGuanModel::alias('guan')
                        ->field('list.*')
                        ->join('hou_gongzhi zhi','zhi.id=guan.gongzhiid')
                        ->join('piao_shou piao','piao.id=guan.piaoid')
                        ->where(['zhi.comid'=>$this->userinfo['comid'],'zhi.deleted_user'=>0,'zhi.isturn'=>1,'zhi.status'=>1])
                        ->where(['piao.comid'=>$this->userinfo['comid'],'piao.deleted_user'=>0,'piao.isturn'=>1,'piao.status'=>1])
                        ->where(['guan.gongzhilistid'=>$listInfo['id']])
                        ->sum('difee');
                    if($item['yifufee'] <= 0){
                        $item['statename'] = '未收票';
                    }else{
                        if($item['yifufee'] < $listInfo['money']){
                            $item['statename'] = '部分收票';
                        }else{
                            $item['statename'] = '已收票';
                        }
                    }
                }

                // 前期费支付【12】
                if($item['modtype'] == 12){
                    $listInfo = QianZhiListModel::where(['id'=>$item['listid']])->find();
                    $item['yifufee'] = QianZhiGuanModel::alias('guan')
                        ->field('list.*')
                        ->join('qian_zhi zhi','zhi.id=guan.zhiid')
                        ->join('qian_piao piao','piao.id=guan.piaoid')
                        ->where(['zhi.comid'=>$this->userinfo['comid'],'zhi.deleted_user'=>0,'zhi.isturn'=>1,'zhi.status'=>1])
                        ->where(['piao.comid'=>$this->userinfo['comid'],'piao.deleted_user'=>0,'piao.isturn'=>1,'piao.status'=>1])
                        ->where(['guan.zhilistid'=>$listInfo['id']])
                        ->sum('difee');
                    if($item['yifufee'] <= 0){
                        $item['statename'] = '未收票';
                    }else{
                        if($item['yifufee'] < $listInfo['money']){
                            $item['statename'] = '部分收票';
                        }else{
                            $item['statename'] = '已收票';
                        }
                    }
                }

                // 储备进项支付【16】
                if($item['modtype'] == 16){
                    $listInfo = HouChujinListModel::where(['id'=>$item['listid']])->find();
                    $item['yifufee'] = HouChujinGuanModel::alias('guan')
                        ->field('list.*')
                        ->join('hou_chujin jin','jin.id=guan.chujinid')
                        ->join('piao_chu piao','piao.id=guan.piaoid')
                        ->where(['jin.comid'=>$this->userinfo['comid'],'jin.deleted_user'=>0,'jin.isturn'=>1,'jin.status'=>1])
                        ->where(['piao.comid'=>$this->userinfo['comid'],'piao.deleted_user'=>0,'piao.isturn'=>1,'piao.status'=>1])
                        ->where(['guan.chujinlistid'=>$listInfo['id']])
                        ->sum('difee');

                    if($item['yifufee'] <= 0){
                        $item['statename'] = '未收票';
                    }else{
                        if($item['yifufee'] < $listInfo['money']){
                            $item['statename'] = '部分收票';
                        }else{
                            $item['statename'] = '已收票';
                        }
                    }
                }

                // 日常付款【19】
                if($item['modtype'] == 19){
                    $listInfo = HouDayzhiListModel::where(['id'=>$item['listid']])->find();
                    $item['yifufee'] = HouDayzhiGuanModel::alias('guan')
                        ->field('list.*')
                        ->join('hou_dayzhi zhi','zhi.id=guan.dayzhiid')
                        ->join('piao_shou piao','piao.id=guan.piaoid')
                        ->where(['zhi.comid'=>$this->userinfo['comid'],'zhi.deleted_user'=>0,'zhi.isturn'=>1,'zhi.status'=>1])
                        ->where(['piao.comid'=>$this->userinfo['comid'],'piao.deleted_user'=>0,'piao.isturn'=>1,'piao.status'=>1])
                        ->where(['guan.dayzhilistid'=>$listInfo['id']])
                        ->sum('difee');
                    if($item['yifufee'] <= 0){
                        $item['statename'] = '未收票';
                    }else{
                        if($item['yifufee'] < $listInfo['money']){
                            $item['statename'] = '部分收票';
                        }else{
                            $item['statename'] = '已收票';
                        }
                    }
                }

                return $item;
            })->toArray();

        return outputJson(2000,lang('code_2000'),$data);
    }
    public function show_account_list(Request $request){
        $id = $request->post('id', 0, 'int');
        $data['data'] = AccountListModel::get_info_by_pk($id);
        if(!$data['data']){
            return outputJson(4000, lang('code_4000'));
        }

        $kuan = AcckuanModel::where(['id'=>$data['data']['kuanid']])->find();
        $data['kuan'] = $kuan;

        $bill = FlowBillModel::where(['id'=>$kuan['billid']])->find();
        $data['bill'] = $bill;

        $account = AccountModel::get_info_by_pk($data['data']['accid']);
        $data['account'] = $account;

        $data['data']['enginname'] = '';
        if($kuan['bidid'] > 0){
            $data['data']['enginname'] = BidModel::where(['id'=>$kuan['bidid']])->value('name');
        }
        if($kuan['enginid'] > 0){
            $data['data']['enginname'] = EnginModel::where(['id'=>$kuan['enginid']])->value('name');
        }
        $data['data']['type_name'] = $kuan['modtype'] > 10 ? '支出' : '收入';
        $data['data']['modtype_name'] = (new KuanService())->get_acckuan_modtype_name($kuan['modtype']);


        $data['files'] = (new OptService())->get_file('account_list', $data['data']['id']);
        return outputJson(2000,lang('code_2000'), $data);
    }
    public function detail_account_list(Request $request){
        $param = $request->post();
        get_page($param);

        $where[] = ['list.comid','=',$this->userinfo['comid']];
        $where[] = ['list.kuanid','=',$param['kuanid']];


        if(isset($param['keyword']) && !empty($param['keyword'])){
            $where[] = ['bill.sericnum|kuan.zhai|kuai.kuanlei|kuan.duier|kuan.incdaotype|list.jiaonum|list.baner','like', '%'.$param['keyword'].'%'];
        }
        if(isset($param['modtype']) && !empty($param['modtype'])){
            $where[] = ['kuan.modtype','=', $param['modtype']];
        }
        if(isset($param['sericnum']) && !empty($param['sericnum'])){
            $where[] = ['bill.sericnum','like', '%'.$param['sericnum'].'%'];
        }
        if(isset($param['zhai']) && !empty($param['zhai'])){
            $where[] = ['kuan.zhai','like', '%'.$param['zhai'].'%'];
        }
        if(isset($param['kuanlei']) && !empty($param['kuanlei'])){
            $where[] = ['kuan.kuanlei','like', '%'.$param['kuanlei'].'%'];
        }
        if(isset($param['duier']) && !empty($param['duier'])){
            $where[] = ['kuan.duier','like', '%'.$param['duier'].'%'];
        }
        if(isset($param['dt']) && !empty($param['dt'])){
            $where[] = ['kuan.dt','between', explode(' - ', $param['dt'])];
        }
        if(isset($param['incdaotype']) && !empty($param['incdaotype'])){
            $where[] = ['kuan.incdaotype','like', '%'.$param['incdaotype'].'%'];
        }
        if(isset($param['jiaonum']) && !empty($param['jiaonum'])){
            $where[] = ['list.jiaonum','like', '%'.$param['jiaonum'].'%'];
        }
        if(isset($param['baner']) && !empty($param['baner'])){
            $where[] = ['list.baner','like', '%'.$param['baner'].'%'];
        }

        $whereEngin = "";
        if(isset($param['enginname']) && !empty($param['enginname'])){
            $whereEngin = "b.name like '%".$param['enginname']."%' or e.name like '%".$param['enginname']."%'";
        }

        $data['modtype'] = (new KuanService())->get_acckuan_modtype(1);
        $data['is_modtype'] = isset($param['modtype']) ? $param['modtype'] : '';


        $data['data'] = AccountListModel::alias('list')
            ->field('list.*,b.name as bidname,e.name as enginname,acc.name as accname,acc.accnum,
            bill.sericnum,bill.status,bill.modenum,bill.mid,
            kuan.modtype,kuan.bidid,kuan.enginid,kuan.zhai,kuan.kuanlei,kuan.duier,kuan.incdaotype')
            ->join('acckuan kuan','kuan.id=list.kuanid')
            ->leftJoin('bid b','b.id=kuan.bidid')
            ->leftJoin('engin e','e.id=kuan.enginid')
            ->leftJoin('account acc','acc.id=list.accid')
            ->join('flow_bill bill','bill.id=kuan.billid')
            ->where($where)
            ->where($whereEngin)
            ->order('list.id DESC')
            ->paginate($param['pagesize'],false,['page'=>$param['page']])
            ->each(function ($item,$key){
                $item['type_name'] = $item['modtype'] > 10 ? '支出' : '收入';
                if($item['bidid'] > 0){
                    $item['enginname'] = $item['bidname'];
                }
                $item['modtype_name'] = (new KuanService())->get_acckuan_modtype_name($item['modtype']);
                $item['accid_name'] = $item['accname'].'('.$item['accnum'].')';
                return $item;
            })->toArray();
        return outputJson(2000,lang('code_2000'),$data);
    }
    public function add_account_list(Request $request){
        if($request->isPost()){
            $param = $request->post();

            if(empty($param['money'])){
                return outputJson(4000, '请输入本次支付金额');
            }
            if(empty($param['accid'])){
                return outputJson(4000, '请选择支付账户');
            }
            if(empty($param['dt'])){
                return outputJson(4000, '请选择交易日期');
            }
            if(empty($param['baner'])){
                return outputJson(4000, '请输入经手人');
            }

            $param['jiaonum'] = (new PaymentService())->create_account_list_jiaonum_zhi();
            $kuan = AcckuanModel::where(['id'=>$param['kuanid']])->find();
            $nofee = round($kuan['money'] - $kuan['payfee'], 2);
            if($param['money'] > $nofee){
                return outputJson(4000, '本次支付金额不能大于未付金额');
            }


            $param['modtype'] = $kuan['modtype'];
            $param['comid'] = $this->userinfo['comid'];
            $param['created_user'] = $param['updated_user'] = $this->userinfo['id'];
            $param['created_at'] = $param['updated_at']= date('Y-m-d H:i:s');

            $files = array_filter($param['files']);
            unset($param['files']);
            unset($param['token']);

            Db::startTrans();
            try {
                $id = AccountListModel::insertGetId($param);

                $xinPayfee = AccountListModel::where(['kuanid'=>$param['kuanid']])->sum('money');
                if($kuan['money'] > 0){
                    if($xinPayfee > 0){
                        if($xinPayfee >= $kuan['money']){
                            $xinPaystate = 2;
                        }else{
                            $xinPaystate = 1;
                        }
                    }else{
                        $xinPaystate = 0;
                    }
                }else{
                    $xinPaystate = 2;
                }
                AcckuanModel::where(['id'=>$param['kuanid']])->update(['paystate'=>$xinPaystate,'payfee'=>$xinPayfee]);

                (new OptService())->relation_file($files,'account_list',$id);
                (new OptService())->add_flow_log(FlowLogModel::SET_ADD, 'account_list', $id);
                (new OptService())->add_log_list(FlowLogModel::SET_ADD, 'account_list', $id, '交易记录');
                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,lang('code_4000'));
            }
            return outputJson(2000,lang('code_2000'));
        }else{
            $kuanid = $request->get('kuanid',0,'int');
            $kuan = AcckuanModel::where(['id'=>$kuanid])->find();
            if(!$kuan){
                return outputJson(4000, lang('code_4000'));
            }
            $data['kuan'] = $kuan;
            $data['bill'] = FlowBillModel::where(['comid'=>$this->userinfo['comid'],'id'=>$kuan['billid']])->find();
            if($kuan['bidid'] > 0){
                $kuan['enginname'] = BidModel::where(['id'=>$kuan['bidid']])->value('name');
            }else{
                $kuan['enginname'] = EnginModel::where(['id'=>$kuan['enginid']])->value('name');
            }
            $kuan['nofee'] = round($kuan['money'] - $kuan['payfee'], 2);

            $data['dt'] = date("Y-m-d");
            $data['baner'] = $this->userinfo['realname'];
            $data['jiaonum'] = (new PaymentService())->create_account_list_jiaonum_zhi();
            $data['files'] = (new OptService())->get_file('account_list');
            return outputJson(2000,lang('code_2000'), $data);
        }
    }
    public function edit_account_list(Request $request){
        if($request->isPost()){
            $param = $request->post();

            if(empty($param['money'])){
                return outputJson(4000, '请输入本次支付金额');
            }
            if(empty($param['accid'])){
                return outputJson(4000, '请选择支付账户');
            }
            if(empty($param['dt'])){
                return outputJson(4000, '请选择交易日期');
            }
            if(empty($param['baner'])){
                return outputJson(4000, '请输入经手人');
            }

            $oldinfo = AccountListModel::where(['id'=>$param['id']])->find();
            $kuan = AcckuanModel::where(['id'=>$oldinfo['kuanid']])->find();
            $kuan['payfee'] = AccountListModel::where(['kuanid'=>$oldinfo['kuanid']])->where('id','neq',$param['id'])->sum('money');
            $nofee = round($kuan['money'] - $kuan['payfee'], 2);
            if($param['money'] > $nofee){
                return outputJson(4000, '本次支付金额不能大于未付金额');
            }


            $param['comid'] = $this->userinfo['comid'];
            $param['created_user'] = $param['updated_user'] = $this->userinfo['id'];
            $param['created_at'] = $param['updated_at']= date('Y-m-d H:i:s');

            $files = array_filter($param['files']);
            unset($param['files']);
            unset($param['token']);

            Db::startTrans();
            try {
                AccountListModel::update($param);

                $xinPayfee = AccountListModel::where(['kuanid'=>$oldinfo['kuanid']])->sum('money');
                if($kuan['money'] > 0){
                    if($xinPayfee > 0){
                        if($xinPayfee >= $kuan['money']){
                            $xinPaystate = 2;
                        }else{
                            $xinPaystate = 1;
                        }
                    }else{
                        $xinPaystate = 0;
                    }
                }else{
                    $xinPaystate = 2;
                }
                AcckuanModel::where(['id'=>$oldinfo['kuanid']])->update(['paystate'=>$xinPaystate,'payfee'=>$xinPayfee]);

                (new OptService())->relation_file($files,'account_list',$param['id']);
                (new OptService())->add_flow_log(FlowLogModel::SET_EDIT, 'account_list', $param['id']);
                (new OptService())->add_log_list(FlowLogModel::SET_EDIT, 'account_list', $param['id'], '交易记录');
                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,lang('code_4000'));
            }
            return outputJson(2000,lang('code_2000'));
        }else{
            $id = $request->get('id',0,'int');
            $data['data'] = AccountListModel::where(['id'=>$id])->find();
            if(!$data['data']){
                return outputJson(4000, lang('code_4000'));
            }


            $kuan = AcckuanModel::where(['id'=>$data['data']['kuanid']])->find();
            $data['kuan'] = $kuan;
            $data['bill'] = FlowBillModel::where(['comid'=>$this->userinfo['comid'],'id'=>$kuan['billid']])->find();
            if($kuan['bidid'] > 0){
                $kuan['enginname'] = BidModel::where(['id'=>$kuan['bidid']])->value('name');
            }else{
                $kuan['enginname'] = EnginModel::where(['id'=>$kuan['enginid']])->value('name');
            }
            $kuan['nofee'] = round($kuan['money'] - $kuan['payfee'], 2);

            $account = AccountModel::get_info_by_pk($data['data']['accid']);
            $data['account'] = $account;

            $data['files'] = (new OptService())->get_file('account_list', $data['data']['id']);
            return outputJson(2000,lang('code_2000'), $data);
        }
    }
    public function mulpay_account_list(Request $request){
        if($request->isPost()){
            $param = $request->post();
            $list = array_filter($param['list']);

            $kknum = 1;
            foreach ($list as &$vvv){
                $kuan = AcckuanModel::where(['id'=>$vvv['kuanid']])->find();
                $nofee = round($kuan['money'] - $kuan['payfee'], 2);
                if($vvv['money'] > $nofee){
                    return outputJson(4000, "第".$kknum."行 本次支付金额不能大于未付金额");
                }

                $vvv['modtype'] = $kuan['modtype'];
                $vvv['comid'] = $this->userinfo['comid'];
                $vvv['created_user'] = $vvv['updated_user'] = $this->userinfo['id'];
                $vvv['created_at'] = $vvv['updated_at']= date('Y-m-d H:i:s');

                $kknum++;
            }


            Db::startTrans();
            try {

                foreach ($list as $item){
                    $files = $item['files'];
                    unset($item['files']);
                    $kuan = AcckuanModel::where(['id'=>$item['kuanid']])->find();

                    $item['jiaonum'] = (new PaymentService())->create_account_list_jiaonum_zhi();

                    $id = AccountListModel::insertGetId($item);

                    $xinPayfee = AccountListModel::where(['kuanid'=>$item['kuanid']])->sum('money');
                    if($kuan['money'] > 0){
                        if($xinPayfee > 0){
                            if($xinPayfee >= $kuan['money']){
                                $xinPaystate = 2;
                            }else{
                                $xinPaystate = 1;
                            }
                        }else{
                            $xinPaystate = 0;
                        }
                    }else{
                        $xinPaystate = 2;
                    }
                    AcckuanModel::where(['id'=>$item['kuanid']])->update(['paystate'=>$xinPaystate,'payfee'=>$xinPayfee]);


                    if(isset($files) && $files){
                        FileModel::where('id','in',$files)->update(['mid'=>$id,'table'=>'account_list']);
                    }
                    (new OptService())->add_flow_log(FlowLogModel::SET_ADD, 'account_list', $id);
                    (new OptService())->add_log_list(FlowLogModel::SET_ADD, 'account_list', $id, '交易记录');

                }

                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,lang('code_4000'));
            }
            return outputJson(2000,lang('code_2000'));
        }else{
            $kuanids = $request->get('ids','','string');
            $list = AcckuanModel::where('id','in',$kuanids)->select()->toArray();

            foreach ($list as &$item){
                $item['bill'] = FlowBillModel::where(['comid'=>$this->userinfo['comid'],'id'=>$item['billid']])->find();
                if($item['bidid'] > 0){
                    $item['enginname'] = BidModel::where(['id'=>$item['bidid']])->value('name');
                }else{
                    $item['enginname'] = EnginModel::where(['id'=>$item['enginid']])->value('name');
                }
                $item['nofee'] = round($item['money'] - $item['payfee'], 2);
            }
            $data['list'] = $list;

            $data['dt'] = date("Y-m-d");
            $data['baner'] = $this->userinfo['realname'];
            $data['files'] = (new OptService())->get_file('account_list');
            return outputJson(2000,lang('code_2000'), $data);
        }
    }
    public function delete_account_list(Request $request){
        $param = $request->post();
        $data = AccountListModel::get_info_by_pk($param['id']);
        if(!$data){
            return outputJson(4000,lang('code_4000'));
        }

        Db::startTrans();
        try {
            AccountListModel::where(['id'=>$data['id']])->delete();

            $kuan = AcckuanModel::where(['id'=>$data['kuanid']])->find();
            $xinPayfee = AccountListModel::where(['kuanid'=>$data['kuanid']])->sum('money');
            if($kuan['money'] > 0){
                if($xinPayfee > 0){
                    if($xinPayfee >= $kuan['money']){
                        $xinPaystate = 2;
                    }else{
                        $xinPaystate = 1;
                    }
                }else{
                    $xinPaystate = 0;
                }
            }else{
                $xinPaystate = 2;
            }
            AcckuanModel::where(['id'=>$data['kuanid']])->update(['paystate'=>$xinPaystate,'payfee'=>$xinPayfee]);

            (new OptService())->add_log_list(FlowLogModel::SET_DELETE, 'account_list', $data['id'], '交易记录');
            Db::commit();
        } catch (\Exception $e) {
            Db::rollback();
            return outputJson(4000,lang('code_4000'));
        }
        return outputJson(2000,lang('code_2000'));
    }






    public function list_accday(Request $request){
        $param = $request->post();
        get_page($param);

        $where[] = ['day.deleted_user','=', 0];
        $where[] = ['day.comid','=',$this->userinfo['comid']];


        if(isset($param['dt']) && !empty($param['dt'])){
            $where[] = ['day.dt','between', explode(' - ', $param['dt'])];
        }

        $data['data'] = AccdayModel::alias('day')
            ->field('day.*')
            ->where($where)
            ->where((new MenuService())->get_miss_where('accday', 'day'))
            ->order('day.id DESC')
            ->paginate($param['pagesize'],false,['page'=>$param['page']])
            ->each(function ($item,$key){
                $item['created_user_name'] = UserModel::get_realname_by_pk($item['created_user']);
                return $item;
            })->toArray();

        $data['curdt'] = date("Y-m-d");
        $data['butauth'] = (new MenuService())->get_butauth('accday');
        return outputJson(2000,lang('code_2000'),$data);
    }
    public function two_accday(Request $request){
        $param = $request->post();
        get_page($param);

        $where[] = ['two.accdayid','=',$param['id']];
        $where[] = ['two.acctypename','neq','E虚拟账户'];


        $data['data'] = AccdayTwoModel::alias('two')
            ->field('two.*')
            ->where($where)
            ->order('two.id DESC')
            ->paginate($param['pagesize'],false,['page'=>$param['page']])
            ->each(function ($item,$key){
                $item['created_user_name'] = UserModel::get_realname_by_pk($item['created_user']);
                $item['accname_hao'] = substr($item['accname'], strrpos($item['accname'], "("));
                $item['accname_zhang'] = str_replace($item['accname_hao'], " ", $item['accname']);

                return $item;
            })->toArray();
        return outputJson(2000,lang('code_2000'),$data);
    }
    public function add_accday(Request $request){
        if($request->isPost()){
            $param = $request->post();

            $accmoney = 0;
            $accIdArr = [];
            $accNameArr = [];
            $accday_two = [];
            $accInfo = AccountModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0])->select()->toArray();
            foreach ($accInfo as $v){
                $accmoney += $v['accmoney'];
                $accIdArr[] = $v['id'];
                $accNameArr[$v['id']] = $v['name'].'('.$v['accnum'].')';
                $accday_two[$v['id']] = [
                    'accname' => $v['name'].'('.$v['accnum'].')',
                    'chanidname' => DeptModel::get_name_by_pk($v['chanid']),
                    'kainame' => $v['accname'],
                    'acctypename' => (new OptService())->get_base_showname('cai_account_acctype', $v['acctype']),
                    'enginname' => EnginModel::where(['id'=>$v['enginid']])->value('name'),
                    'dt' => date("Y-m-d"),
                    'shangmoney' => 0,
                    'shoufee' => 0,
                    'zhifee' => 0,
                    'money' => 0,
                    'accmoney' => $v['accmoney'],
                    'created_user' => $this->userinfo['id'],
                    'created_at' => date('Y-m-d H:i:s'),
                ];
            }


            $shang_shou = AccountListModel::where(['comid'=>$this->userinfo['comid']])->where('accid','in',$accIdArr)->where('modtype','<',10)
                ->where('created_at','<',date("Y-m-d 00:00:00"))->sum('money');
            $shang_zhi = AccountListModel::where(['comid'=>$this->userinfo['comid']])->where('accid','in',$accIdArr)->where('modtype','>',10)
                ->where('created_at','<',date("Y-m-d 00:00:00"))->sum('money');
            //上日余额
            $shangmoney = round($accmoney + $shang_shou - $shang_zhi, 2);


            //今日交易信息
            $todayinfo = AccountListModel::alias('list')
                ->field('list.kuanid,list.money,list.accid,list.dt,list.modtype,list.explain,kuan.billid
                ,kuan.bidid,kuan.enginid,kuan.zhai,kuan.kuanlei,kuan.duier,kuan.mid,kuan.listid')
                ->join('acckuan kuan','kuan.id=list.kuanid')
                ->where(['list.comid'=>$this->userinfo['comid']])
                ->where('list.accid','in',$accIdArr)
                ->where('list.created_at','>=',date("Y-m-d 00:00:00"))
                ->select()->toArray();

            $today_shou = 0;
            $today_zhi = 0;
            $accday_list = [];
            foreach ($todayinfo as $vv){
                if($vv['modtype'] < 10){
                    $accday_two[$vv['accid']]['shoufee'] += $vv['money'];
                    $today_shou += $vv['money'];
                }else{
                    $accday_two[$vv['accid']]['zhifee'] += $vv['money'];
                    $today_zhi += $vv['money'];
                }


                $enginname = '';
                $gui_unit = '';
                if($vv['bidid'] > 0){
                    $enginInfo = BidModel::where(['id'=>$vv['bidid']])->find();
                    $enginname = $enginInfo['name'];
                    $gui_unit = DeptModel::get_name_by_pk($enginInfo['gui_unit']);
                }
                if($vv['enginid'] > 0){
                    $enginInfo = EnginModel::where(['id'=>$vv['enginid']])->find();
                    $enginname = $enginInfo['name'];
                    $gui_unit = DeptModel::get_name_by_pk($enginInfo['gui_unit']);
                }


                $piaoList = [];
                //工程回款【2】
                if($vv['modtype'] == 2){
                    $gonghuilist = HouGonghuiListModel::where(['gonghuiid'=>$vv['mid']])->select()->toArray();
                    foreach ($gonghuilist as $gonghuivv){
                        $piaoKaiDt = PiaoKaiListModel::alias('list')
                            ->join('piao_kai zhu','zhu.id=list.kaiid')
                            ->where(['zhu.deleted_user'=>0,'zhu.isturn'=>1,'zhu.status'=>1])
                            ->where(['list.id'=>$gonghuivv['piaoid']])
                            ->value('zhu.dt');
                        if(!$piaoKaiDt || $piaoKaiDt == '0000-00-00'){
                            $piaoKaiDt = '';
                        }
                        $piaoList[] = ['dt'=>$piaoKaiDt, 'money'=>$gonghuivv['xiaofee']];
                    }
                }
                //工程付款【11】
                if($vv['modtype'] == 11){
                    $gongzhiguan = HouGongzhiGuanModel::alias('guan')
                        ->field('guan.*')
                        ->join('hou_gongzhi zhi','zhi.id=guan.gongzhiid')
                        ->join('piao_shou piao','piao.id=guan.piaoid')
                        ->where(['zhi.comid'=>$this->userinfo['comid'],'zhi.deleted_user'=>0,'zhi.isturn'=>1,'zhi.status'=>1])
                        ->where(['piao.comid'=>$this->userinfo['comid'],'piao.deleted_user'=>0,'piao.isturn'=>1,'piao.status'=>1])
                        ->where(['guan.gongzhiid'=>$vv['mid'],'guan.gongzhilistid'=>$vv['listid']])
                        ->select()->toArray();
                    foreach ($gongzhiguan as $gongzhiguanvv){
                        $piaoKaiDt = PiaoShouListModel::alias('list')
                            ->join('piao_shou zhu','zhu.id=list.shouid')
                            ->where(['zhu.deleted_user'=>0,'zhu.isturn'=>1,'zhu.status'=>1])
                            ->where(['list.id'=>$gongzhiguanvv['piaolistid']])
                            ->value('list.kaidt');
                        if(!$piaoKaiDt || $piaoKaiDt == '0000-00-00'){
                            $piaoKaiDt = '';
                        }
                        $piaoList[] = ['dt'=>$piaoKaiDt, 'money'=>$gongzhiguanvv['difee']];
                    }
                }

                //前期费支付【12】
                if($vv['modtype'] == 12){
                    $qianzhiguan = QianZhiGuanModel::alias('guan')
                        ->field('guan.*')
                        ->join('qian_zhi zhi','zhi.id=guan.zhiid')
                        ->join('qian_piao piao','piao.id=guan.piaoid')
                        ->where(['zhi.comid'=>$this->userinfo['comid'],'zhi.deleted_user'=>0,'zhi.isturn'=>1,'zhi.status'=>1])
                        ->where(['piao.comid'=>$this->userinfo['comid'],'piao.deleted_user'=>0,'piao.isturn'=>1,'piao.status'=>1])
                        ->where(['guan.zhiid'=>$vv['mid'],'guan.zhilistid'=>$vv['listid']])
                        ->select()->toArray();
                    foreach ($qianzhiguan as $qianzhiguanvv){
                        $piaoKaiDt = QianPiaoListModel::alias('list')
                            ->join('qian_piao zhu','zhu.id=list.piaoid')
                            ->where(['zhu.deleted_user'=>0,'zhu.isturn'=>1,'zhu.status'=>1])
                            ->where(['list.id'=>$qianzhiguanvv['piaolistid']])
                            ->value('list.kaidt');
                        if(!$piaoKaiDt || $piaoKaiDt == '0000-00-00'){
                            $piaoKaiDt = '';
                        }
                        $piaoList[] = ['dt'=>$piaoKaiDt, 'money'=>$qianzhiguanvv['difee']];
                    }
                }
                //储备进项支付【16】
                if($vv['modtype'] == 16){
                    $chujinguan = HouChujinGuanModel::alias('guan')
                        ->field('guan.*')
                        ->join('hou_chujin jin','jin.id=guan.chujinid')
                        ->join('piao_chu piao','piao.id=guan.piaoid')
                        ->where(['jin.comid'=>$this->userinfo['comid'],'jin.deleted_user'=>0,'jin.isturn'=>1,'jin.status'=>1])
                        ->where(['piao.comid'=>$this->userinfo['comid'],'piao.deleted_user'=>0,'piao.isturn'=>1,'piao.status'=>1])
                        ->where(['guan.chujinid'=>$vv['mid'],'guan.chujinlistid'=>$vv['listid']])
                        ->select()->toArray();
                    foreach ($chujinguan as $chujinguanvv){
                        $piaoKaiDt = PiaoChuListModel::alias('list')
                            ->join('piao_chu zhu','zhu.id=list.piaochuid')
                            ->where(['zhu.deleted_user'=>0,'zhu.isturn'=>1,'zhu.status'=>1])
                            ->where(['list.id'=>$chujinguanvv['piaolistid']])
                            ->value('list.kaidt');
                        if(!$piaoKaiDt || $piaoKaiDt == '0000-00-00'){
                            $piaoKaiDt = '';
                        }
                        $piaoList[] = ['dt'=>$piaoKaiDt, 'money'=>$chujinguanvv['difee']];
                    }
                }

                //日常付款【19】
                if($vv['modtype'] == 19){
                    $dayzhiguan = HouDayzhiGuanModel::alias('guan')
                        ->field('guan.*')
                        ->join('hou_dayzhi zhi','zhi.id=guan.dayzhiid')
                        ->join('piao_day piao','piao.id=guan.piaoid')
                        ->where(['zhi.comid'=>$this->userinfo['comid'],'zhi.deleted_user'=>0,'zhi.isturn'=>1,'zhi.status'=>1])
                        ->where(['piao.comid'=>$this->userinfo['comid'],'piao.deleted_user'=>0,'piao.isturn'=>1,'piao.status'=>1])
                        ->where(['guan.dayzhiid'=>$vv['mid'],'guan.dayzhilistid'=>$vv['listid']])
                        ->select()->toArray();
                    foreach ($dayzhiguan as $dayzhiguanvv){
                        $piaoKaiDt = PiaoChuListModel::alias('list')
                            ->join('piao_chu zhu','zhu.id=list.piaochuid')
                            ->where(['zhu.deleted_user'=>0,'zhu.isturn'=>1,'zhu.status'=>1])
                            ->where(['list.id'=>$dayzhiguanvv['piaolistid']])
                            ->value('list.kaidt');
                        if(!$piaoKaiDt || $piaoKaiDt == '0000-00-00'){
                            $piaoKaiDt = '';
                        }
                        $piaoList[] = ['dt'=>$piaoKaiDt, 'money'=>$dayzhiguanvv['difee']];
                    }
                }



                foreach ($accday_two as $twoKK=>&$twoVV){
                    $wang_shou = AccountListModel::where(['comid'=>$this->userinfo['comid'],'accid'=>$twoKK])->where('modtype','<',10)
                        ->where('created_at','<',date("Y-m-d 00:00:00"))->sum('money');
                    $wang_zhi = AccountListModel::where(['comid'=>$this->userinfo['comid'],'accid'=>$twoKK])->where('modtype','>',10)
                        ->where('created_at','<',date("Y-m-d 00:00:00"))->sum('money');
                    $twoVV['shangmoney'] = round($twoVV['accmoney'] + $wang_shou - $wang_zhi, 2);
                    $twoVV['money'] = round($twoVV['shangmoney'] + $twoVV['shoufee'] - $twoVV['zhifee'], 2);
                }



                $accday_list[] = [
                    'billid' => $vv['billid'],
                    'mid' => $vv['mid'],
                    'listid' => $vv['listid'],
                    'modtype' => $vv['modtype'],
                    'accname' => $accNameArr[$vv['accid']],
                    'dt' => $vv['dt'],
                    'duier' => $vv['duier'],
                    'kuanlei' => $vv['kuanlei'],
                    'zhai' => $vv['zhai'],
                    'enginname' => $enginname,
                    'gui_unit' => $gui_unit,
                    'money' => $vv['money'],
                    'explain' => $vv['explain'],
                    'piao' => $piaoList,
                ];
            }

            $today_money = round($shangmoney + $today_shou - $today_zhi, 2);


            $param['comid'] = $this->userinfo['comid'];
            $param['created_user'] = $param['updated_user'] = $this->userinfo['id'];
            $param['created_at'] = $param['updated_at']= date('Y-m-d H:i:s');
            $param['deptid'] = $this->userinfo['deptid'];
            $param['chanid'] = (new UserService())->get_user_chanid();


            Db::startTrans();
            try {
                $exist = AccdayModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0,'dt'=>date("Y-m-d")])->find();
                if($exist){
                    AccdayModel::where(['id'=>$exist['id']])->update([
                        'comid' => $param['comid'],
                        'created_user' => $param['created_user'],
                        'updated_user' => $param['created_user'],
                        'created_at' => $param['created_at'],
                        'updated_at' => $param['created_at'],
                        'deptid' => $param['deptid'],
                        'chanid' => $param['chanid'],
                        'dt' => date("Y-m-d"),
                        'shangmoney' => $shangmoney,
                        'shoufee' => $today_shou,
                        'zhifee' => $today_zhi,
                        'money' => $today_money,
                    ]);
                    $accdayid = $exist['id'];
                }else{
                    $accdayid = AccdayModel::insertGetId([
                        'comid' => $param['comid'],
                        'created_user' => $param['created_user'],
                        'updated_user' => $param['created_user'],
                        'created_at' => $param['created_at'],
                        'updated_at' => $param['created_at'],
                        'deptid' => $param['deptid'],
                        'chanid' => $param['chanid'],
                        'dt' => date("Y-m-d"),
                        'shangmoney' => $shangmoney,
                        'shoufee' => $today_shou,
                        'zhifee' => $today_zhi,
                        'money' => $today_money,
                    ]);
                }

                AccdayTwoModel::where(['accdayid'=>$accdayid])->delete();
                foreach ($accday_two as $itemTwo){
                    $itemTwo['accdayid'] = $accdayid;
                    unset($itemTwo['accmoney']);
                    AccdayTwoModel::insert($itemTwo);
                }


                AccdayListModel::where(['accdayid'=>$accdayid])->delete();
                AccdayPiaoModel::where(['accdayid'=>$accdayid])->delete();
                foreach ($accday_list as $item){
                    $item['accdayid'] = $accdayid;
                    $piaoList = $item['piao'];
                    unset($item['piao']);

                    $accdaylistid = AccdayListModel::insertGetId($item);
                    foreach ($piaoList as $piaoVV){
                        $piaoVV['accdayid'] = $accdayid;
                        $piaoVV['accdaylistid'] = $accdaylistid;
                        AccdayPiaoModel::insertGetId($piaoVV);
                    }
                }



                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,lang('code_4000'));
            }
            return outputJson(2000,lang('code_2000'));
        }else{
            $data['exist'] = false;
            $exist = AccdayModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0,'dt'=>date("Y-m-d")])->find();
            if($exist){
                $data['exist'] = true;
            }
            return outputJson(2000,lang('code_2000'), $data);
        }
    }
    public function del_accday(Request $request){
        $param = $request->post();
        $data = AccdayModel::get_info_by_pk($param['id']);
        if(!$data){
            return outputJson(4000,lang('code_4000'));
        }

        Db::startTrans();
        try {
            AccdayModel::where(['id'=>$data['id']])->update([
                'deleted_user' => $this->userinfo['id'],
                'deleted_at' => date('Y-m-d H:i:s')
            ]);

            (new OptService())->add_log_list(FlowLogModel::SET_DELETE, 'accday', $data['id'], '资金日报表');
            Db::commit();
        } catch (\Exception $e) {
            Db::rollback();
            return outputJson(4000,lang('code_4000'));
        }
        return outputJson(2000,lang('code_2000'));
    }
    public function show_accday(Request $request){
        $param = $request->post();

        $list = [];
        $accday = AccdayModel::where(['id'=>$param['id']])->find();



        $twoinfo = AccdayTwoModel::where(['accdayid'=>$accday['id'],'accname'=>$param['accname']])->find();
        $list[] = ['shangmoney'=>$twoinfo['shangmoney']];

        $accdayList = AccdayListModel::where(['accdayid'=>$accday['id'],'accname'=>$param['accname']])->select()->toArray();
        foreach ($accdayList as $vvv){
            if($vvv['modtype'] < 10){
                $vvv['shoufee'] = $vvv['money'];
            }else{
                $vvv['zhifee'] = $vvv['money'];
            }
            $vvv['piao'] = AccdayPiaoModel::field('dt as piaodt,money as piaomoney')->where(['accdaylistid'=>$vvv['id']])->select()->toArray();


            $vvv['kainame'] = $twoinfo['kainame'];

            $list[] = $vvv;
        }

        $list[] = [
            'dt'=>'合计',
            'shangmoney'=>$twoinfo['shangmoney'],
            'shoufee'=>$twoinfo['shoufee'],
            'zhifee'=>$twoinfo['zhifee'],
            'todaymoney'=>$twoinfo['money'],
        ];


        foreach ($list as &$item){
            $item['accname_hao'] = '';
            $item['accname_zhang'] = '';
            if(isset($item['accname']) && !empty($item['accname'])){
                $item['accname_hao'] = substr($item['accname'], strrpos($item['accname'], "("));
                $item['accname_zhang'] = str_replace($item['accname_hao'], " ", $item['accname']);
            }
        }


        $data['data'] = $list;
        return outputJson(2000,lang('code_2000'),$data);
    }
    public function down_accday(Request $request){
        if($request->isPost()){
            $param = $request->post();
            $accday = AccdayModel::where(['id'=>$param['id']])->find();

            $excelFileName = '资金日报表'.$accday['dt'];
            $sheetTitle[] = '资金日报表交易明细'.$accday['dt'];

            $fieldArr = [
                'accname'=>'资金账户',
                'kainame'=>'开户行',
                'dt'=>'交易日期',
                'duier'=>'交易对象',
                'kuanlei'=>'款项类型',
                'zhai'=>'款项摘要',
                'enginname'=>'工程名称',
                'gui_unit'=>'业务归属',
                'shangmoney'=>'上日余额(元)',
                'shoufee'=>'本日发生额(元)',
                'zhifee'=>'',
                'todaymoney'=>'本日余额(元)',
                'explain'=>'备注',
            ];
            $fieldArrZheng = [
                'accname'=>'',
                'kainame'=>'开户行',
                'dt'=>'',
                'duier'=>'',
                'kuanlei'=>'',
                'zhai'=>'',
                'enginname'=>'',
                'gui_unit'=>'',
                'shangmoney'=>'',
                'shoufee'=>'转入(贷方)',
                'zhifee'=>'转出(借方)',
                'todaymoney'=>'',
                'explain'=>'',
            ];
            $excelFieldsZHCN[] = [$fieldArr,$fieldArrZheng];
            $list = [];

            $accday = AccdayModel::where(['id'=>$param['id']])->find();
            $list[] = ['shangmoney'=>$accday['shangmoney']];

            $accdayList = AccdayListModel::where(['accdayid'=>$accday['id']])->select()->toArray();
            foreach ($accdayList as $vvv){
                if($vvv['modtype'] < 10){
                    $vvv['shoufee'] = $vvv['money'];
                }else{
                    $vvv['zhifee'] = $vvv['money'];
                }
                $vvv['piao'] = AccdayPiaoModel::field('dt as piaodt,money as piaomoney')->where(['accdaylistid'=>$vvv['id']])->select()->toArray();
                $list[] = $vvv;
            }

            $list[] = [
                'dt'=>'合计',
                'shangmoney'=>$accday['shangmoney'],
                'shoufee'=>$accday['shoufee'],
                'zhifee'=>$accday['zhifee'],
                'todaymoney'=>$accday['money'],
            ];






            $twoData = AccdayTwoModel::alias('two')
                ->field('two.*')
                ->where(['two.accdayid'=>$accday['id']])
                ->order('two.id DESC')
                ->select()->toArray();
            foreach ($twoData as &$ttt){
                $ttt['created_user_name'] = UserModel::get_realname_by_pk($ttt['created_user']);
                $ttt['accname_hao'] = substr($ttt['accname'], strrpos($ttt['accname'], "("));
                $ttt['accname_zhang'] = str_replace($ttt['accname_hao'], " ", $ttt['accname']);
            }
            $twoFieldArr = [
                'chanidname'=>'所属单位',
                'accname_zhang'=>'账户名称',
                'acctypename'=>'账户性质',
                'kainame'=>'开户行',
                'accname_hao'=>'银行账号',
                'enginname'=>'工程名称',
                'dt'=>'日期',
                'shangmoney'=>'上日余额(元)',
                'shoufee'=>'今日收入(元)',
                'zhifee'=>'今日支出(元)',
                'money'=>'今日余额(元)',
                'created_at'=>'生成时间',
                'created_user_name'=>'生成人',
            ];
            $excelFieldsZHCN[] = $twoFieldArr;
            $sheetTitle[] = '资金日报表账户明细'.$accday['dt'];




            deldir('./mainexcel/');
            $this->downaccdayinfo([$list,$twoData], $excelFieldsZHCN, $excelFileName, $sheetTitle);
            return outputJson(2000,lang('code_2000'), 'mainexcel/'.$excelFileName.'.xls');
        }
        return outputJson(2000,lang('code_2000'));
    }


    function downaccdayinfo($list, $excelFieldsZHCN, $excelFileName, $sheetTitle)
    {
        $excelFileName = iconv('UTF-8', 'GBK', $excelFileName);
//        $excelFileName = iconv('UTF-8', 'UTF-8', $excelFileName);
        $excelFileName = $excelFileName;
        require_once '../extend/phpoffice/PHPExcel/PHPExcel.php';
        $objPHPExcel = new \PHPExcel();
        $index = 0;
        $ret = [];


        foreach ($excelFieldsZHCN as $key => $value) {
            if($key == 0){
                $objPHPExcel->createSheet();
                $i = 0;
                $ret = [];
                foreach ($value[0] as $fieldName => $ZHCN) {
                    $pCoordinate = \PHPExcel_Cell::stringFromColumnIndex($i);
                    $objPHPExcel->getActiveSheet()->mergeCells('A1:A2');
                    $objPHPExcel->getActiveSheet()->mergeCells('B1:B2');
                    $objPHPExcel->getActiveSheet()->mergeCells('C1:C2');
                    $objPHPExcel->getActiveSheet()->mergeCells('D1:D2');
                    $objPHPExcel->getActiveSheet()->mergeCells('E1:E2');
                    $objPHPExcel->getActiveSheet()->mergeCells('F1:F2');
                    $objPHPExcel->getActiveSheet()->mergeCells('G1:G2');
                    $objPHPExcel->getActiveSheet()->mergeCells('H1:H2');
                    $objPHPExcel->getActiveSheet()->mergeCells('I1:I2');
                    $objPHPExcel->getActiveSheet()->mergeCells('J1:K1');
                    $objPHPExcel->getActiveSheet()->mergeCells('L1:L2');
                    $objPHPExcel->getActiveSheet()->mergeCells('M1:M2');
                    $objPHPExcel->getActiveSheet()->getColumnDimension($pCoordinate)->setWidth(15);
                    $objPHPExcel->setActiveSheetIndex($index)->setCellValue($pCoordinate . '1', $value[0][$fieldName]);
                    $objPHPExcel->getActiveSheet()->getStyle($pCoordinate . '1')->getFont()->setBold(true);
                    $ret[$i] = $fieldName;
                    $i++;
                }


                $i = 0;
                foreach ($value[1] as $fieldName => $ZHCN) {
                    $pCoordinate = \PHPExcel_Cell::stringFromColumnIndex($i);
//                    $objPHPExcel->getActiveSheet()->getColumnDimension($pCoordinate)->setWidth(15);
                    $objPHPExcel->setActiveSheetIndex($index)->setCellValue($pCoordinate . '2', $value[1][$fieldName]);
                    $objPHPExcel->getActiveSheet()->getStyle($pCoordinate . '2')->getFont()->setBold(true);
                    $ret[$i] = $fieldName;
                    $i++;
                }

                $row = 3;
                if ($list[$key]) {
                    foreach ($list[$key] as $kk=>$item) {
                        $i = 0;
                        foreach ($ret as $field) {
                            $pCoordinate = \PHPExcel_Cell::stringFromColumnIndex($i);
                            if(!isset($item[$field])){
                                $item[$field] = '';
                            }
                            $objPHPExcel->setActiveSheetIndex($index)->setCellValue($pCoordinate . $row, '' . $item[$field]);
                            $i++;
                        }
                        $objPHPExcel->getActiveSheet()->getStyle('C'.($kk+2))->getAlignment()->setWrapText(true);
                        $row++;
                    }
                }
                $objPHPExcel->getActiveSheet()->setTitle($sheetTitle[$key]);
                $objPHPExcel->setActiveSheetIndex($index);
                $datacouns = count($list[$key])+2;
                $objPHPExcel->getActiveSheet()->getStyle('A1:M'.$datacouns)->getAlignment()->setVertical(\PHPExcel_Style_Alignment::VERTICAL_CENTER);//垂直居中
                $objPHPExcel->getActiveSheet()->getStyle('A1:M'.$datacouns)->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);//垂直居中
            }else{
                $objPHPExcel->createSheet();
                $i = 0;
                $ret = [];
                foreach ($value as $fieldName => $ZHCN) {
                    $pCoordinate = \PHPExcel_Cell::stringFromColumnIndex($i);
                    $objPHPExcel->getActiveSheet()->getColumnDimension($pCoordinate)->setWidth(20);
                    $objPHPExcel->setActiveSheetIndex($index)->setCellValue($pCoordinate . '1', $value[$fieldName]);
                    $objPHPExcel->getActiveSheet()->getStyle($pCoordinate . '1')->getFont()->setBold(true);
                    $ret[$i] = $fieldName;
                    $i++;
                }
                $row = 2;
                if ($list[$key]) {
                    foreach ($list[$key] as $kk=>$item) {
                        $i = 0;
                        foreach ($ret as $field) {
                            $pCoordinate = \PHPExcel_Cell::stringFromColumnIndex($i);
                            if(!isset($item[$field])){
                                $item[$field] = '';
                            }
                            $objPHPExcel->setActiveSheetIndex($index)->setCellValue($pCoordinate . $row, '' . $item[$field]);

//                            if(isset($item['key'])){
//                                $objPHPExcel->setActiveSheetIndex($index)->getStyle($pCoordinate . $row)->getFont()->setItalic(true);
//                                $objPHPExcel->setActiveSheetIndex($index)->getStyle($pCoordinate . $row)->getFont()->getColor()->setRGB('808080');
//                            }

                            $i++;
                        }
                        $objPHPExcel->getActiveSheet()->getStyle('C'.($kk+2))->getAlignment()->setWrapText(true);
                        $row++;
                    }
                }

                $objPHPExcel->getActiveSheet()->setTitle($sheetTitle[$key]);
                $objPHPExcel->setActiveSheetIndex($index);
                $datacouns = count($list[$key])+1;
                $objPHPExcel->getActiveSheet()->getStyle('A1:M'.$datacouns)->getAlignment()->setVertical(\PHPExcel_Style_Alignment::VERTICAL_CENTER);//垂直居中


            }



            $index++;
        }
        $objPHPExcel->removeSheetByIndex(count($sheetTitle));

        header('Content-Type: application/vnd.ms-excel');
        header('Content-Disposition: attachment;filename="' . $excelFileName . '.xls"');
        header('Cache-Control: max-age=0');
        header('Cache-Control: max-age=1');
        header('Expires: Mon, 26 Jul 1997 05:00:00 GMT');
        header('Last-Modified: ' . gmdate('D, d M Y H:i:s') . ' GMT');
        header('Cache-Control: cache, must-revalidate');
        header('Pragma: public');
        $objWriter = \PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
        $objWriter->save('mainexcel/'.$excelFileName.'.xls');
    }
    function downaccdayinfo22($list, $excelFieldsZHCN, $excelFileName, $sheetTitle, $fieldArrZheng)
    {
        $excelFileName = iconv('UTF-8', 'GBK', $excelFileName);
//        $excelFileName = iconv('UTF-8', 'UTF-8', $excelFileName);
        $excelFileName = $excelFileName;
        require_once '../extend/phpoffice/PHPExcel/PHPExcel.php';
        $objPHPExcel = new \PHPExcel();
        $index = 0;
        $ret = [];


        foreach ($excelFieldsZHCN as $key => $value) {
            $objPHPExcel->createSheet();
            $i = 0;
            foreach ($value as $fieldName => $ZHCN) {
                $pCoordinate = \PHPExcel_Cell::stringFromColumnIndex($i);
                $objPHPExcel->getActiveSheet()->mergeCells('A1:A2');
                $objPHPExcel->getActiveSheet()->mergeCells('B1:B2');
                $objPHPExcel->getActiveSheet()->mergeCells('C1:C2');
                $objPHPExcel->getActiveSheet()->mergeCells('D1:D2');
                $objPHPExcel->getActiveSheet()->mergeCells('E1:E2');
                $objPHPExcel->getActiveSheet()->mergeCells('F1:F2');
                $objPHPExcel->getActiveSheet()->mergeCells('G1:G2');
                $objPHPExcel->getActiveSheet()->mergeCells('H1:H2');
                $objPHPExcel->getActiveSheet()->mergeCells('I1:I2');
                $objPHPExcel->getActiveSheet()->mergeCells('J1:K1');
                $objPHPExcel->getActiveSheet()->mergeCells('L1:L2');
                $objPHPExcel->getActiveSheet()->mergeCells('M1:M2');
                $objPHPExcel->getActiveSheet()->getColumnDimension($pCoordinate)->setWidth(15);
                $objPHPExcel->setActiveSheetIndex($index)->setCellValue($pCoordinate . '1', $value[$fieldName]);
                $objPHPExcel->getActiveSheet()->getStyle($pCoordinate . '1')->getFont()->setBold(true);
                $ret[$i] = $fieldName;
                $i++;
            }


            $i = 0;
            foreach ($fieldArrZheng as $fieldName => $ZHCN) {
                $pCoordinate = \PHPExcel_Cell::stringFromColumnIndex($i);
                $objPHPExcel->getActiveSheet()->getColumnDimension($pCoordinate)->setWidth(15);
                $objPHPExcel->setActiveSheetIndex($index)->setCellValue($pCoordinate . '2', $fieldArrZheng[$fieldName]);
                $objPHPExcel->getActiveSheet()->getStyle($pCoordinate . '2')->getFont()->setBold(true);
                $ret[$i] = $fieldName;
                $i++;
            }

            $row = 3;
            if ($list[$key]) {
                foreach ($list[$key] as $kk=>$item) {
                    $i = 0;
                    foreach ($ret as $field) {
                        $pCoordinate = \PHPExcel_Cell::stringFromColumnIndex($i);
                        if(!isset($item[$field])){
                            $item[$field] = '';
                        }
                        $objPHPExcel->setActiveSheetIndex($index)->setCellValue($pCoordinate . $row, '' . $item[$field]);
                        $i++;
                    }
                    $objPHPExcel->getActiveSheet()->getStyle('C'.($kk+2))->getAlignment()->setWrapText(true);
                    $row++;
                }
            }
            $objPHPExcel->getActiveSheet()->setTitle($sheetTitle[$key]);
            $objPHPExcel->setActiveSheetIndex($index);
            $datacouns = count($list[$key])+2;
            $objPHPExcel->getActiveSheet()->getStyle('A1:N'.$datacouns)->getAlignment()->setVertical(\PHPExcel_Style_Alignment::VERTICAL_CENTER);//垂直居中
            $objPHPExcel->getActiveSheet()->getStyle('A1:N'.$datacouns)->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);//垂直居中
//            $objPHPExcel->getActiveSheet()->getStyle('A1:N'.$datacouns)->getBorders()->getLeft()->setBorderStyle(\PHPExcel_Style_Border::BORDER_THIN);
//            $objPHPExcel->getActiveSheet()->getStyle('A1:N'.$datacouns)->getBorders()->getRight()->setBorderStyle(\PHPExcel_Style_Border::BORDER_THIN);
//            $objPHPExcel->getActiveSheet()->getStyle('A1:N'.$datacouns)->getBorders()->getTop()->setBorderStyle(\PHPExcel_Style_Border::BORDER_THIN);
//            $objPHPExcel->getActiveSheet()->getStyle('A1:N'.$datacouns)->getBorders()->getBottom()->setBorderStyle(\PHPExcel_Style_Border::BORDER_THIN);
//
//


            $index++;
        }
        $objPHPExcel->removeSheetByIndex(count($sheetTitle));

        header('Content-Type: application/vnd.ms-excel');
        header('Content-Disposition: attachment;filename="' . $excelFileName . '.xls"');
        header('Cache-Control: max-age=0');
        header('Cache-Control: max-age=1');
        header('Expires: Mon, 26 Jul 1997 05:00:00 GMT');
        header('Last-Modified: ' . gmdate('D, d M Y H:i:s') . ' GMT');
        header('Cache-Control: cache, must-revalidate');
        header('Pragma: public');
        $objWriter = \PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
        $objWriter->save('mainexcel/'.$excelFileName.'.xls');
    }






}
